1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
use std::ops::{Deref, DerefMut};
use luz::{connection::Tls, jabber_stream::bound_stream::BoundJabberWriter};
use stanza::client::Stanza;
use tokio::{
sync::{mpsc, oneshot},
task::JoinHandle,
};
use crate::error::WriteError;
/// actor that receives jabber stanzas to write, and if there is an error, sends a message back to the supervisor then aborts, so the supervisor can spawn a new stream.
pub struct Write {
stream: BoundJabberWriter<Tls>,
/// connection session write queue
stanza_receiver: mpsc::Receiver<WriteMessage>,
// control stuff
control_receiver: mpsc::Receiver<WriteControl>,
on_crash: oneshot::Sender<(WriteMessage, WriteState)>,
}
/// when a crash/abort occurs, this gets sent back to the supervisor, possibly with the current write that failed, so that the connection session can continue
pub struct WriteState {
pub stanza_recv: mpsc::Receiver<WriteMessage>,
}
#[derive(Debug)]
pub struct WriteMessage {
pub stanza: Stanza,
pub respond_to: oneshot::Sender<Result<(), WriteError>>,
}
pub enum WriteControl {
Disconnect,
Abort(oneshot::Sender<WriteState>),
}
impl Write {
fn new(
stream: BoundJabberWriter<Tls>,
stanza_receiver: mpsc::Receiver<WriteMessage>,
control_receiver: mpsc::Receiver<WriteControl>,
on_crash: oneshot::Sender<(WriteMessage, WriteState)>,
) -> Self {
Self {
stream,
stanza_receiver,
control_receiver,
on_crash,
}
}
async fn write(&mut self, stanza: &Stanza) -> Result<(), peanuts::Error> {
Ok(self.stream.write(stanza).await?)
}
async fn run_reconnected(mut self, retry_msg: WriteMessage) {
// try to retry sending the message that failed to send previously
let result = self.stream.write(&retry_msg.stanza).await;
match result {
Err(e) => match &e {
peanuts::Error::ReadError(_error) => {
// make sure message is not lost from error, supervisor handles retry and reporting
// TODO: upon reconnect, make sure we are not stuck in a reconnection loop
let _ = self.on_crash.send((
retry_msg,
WriteState {
stanza_recv: self.stanza_receiver,
},
));
return;
}
_ => {
let _ = retry_msg.respond_to.send(Err(e.into()));
}
},
_ => {
let _ = retry_msg.respond_to.send(Ok(()));
}
}
// return to normal loop
self.run().await
}
async fn run(mut self) {
loop {
tokio::select! {
Some(msg) = self.control_receiver.recv() => {
match msg {
WriteControl::Disconnect => {
// close the stanza_receiver channel and drain out all of the remaining stanzas to send
self.stanza_receiver.close();
// TODO: put this in some kind of function to avoid code duplication
while let Some(msg) = self.stanza_receiver.recv().await {
let result = self.stream.write(&msg.stanza).await;
match result {
Err(e) => match &e {
peanuts::Error::ReadError(_error) => {
// if connection lost during disconnection, just send lost connection error to the write requests
let _ = msg.respond_to.send(Err(WriteError::LostConnection));
while let Some(msg) = self.stanza_receiver.recv().await {
let _ = msg.respond_to.send(Err(WriteError::LostConnection));
}
break;
}
// otherwise complete sending all the stanzas currently in the queue
_ => {
let _ = msg.respond_to.send(Err(e.into()));
}
},
_ => {
let _ = msg.respond_to.send(Ok(()));
}
}
}
let _ = self.stream.try_close().await;
break;
},
// in case of abort, stream is already fucked, just send the receiver ready for a reconnection at the same resource
WriteControl::Abort(sender) => {
let _ = sender.send(WriteState { stanza_recv: self.stanza_receiver });
break;
},
}
},
Some(msg) = self.stanza_receiver.recv() => {
let result = self.stream.write(&msg.stanza).await;
match result {
Err(e) => match &e {
peanuts::Error::ReadError(_error) => {
// make sure message is not lost from error, supervisor handles retry and reporting
let _ = self.on_crash.send((msg, WriteState { stanza_recv: self.stanza_receiver }));
break;
}
_ => {
let _ = msg.respond_to.send(Err(e.into()));
}
},
_ => {
let _ = msg.respond_to.send(Ok(()));
}
}
},
else => break,
}
}
}
}
#[derive(Clone)]
pub struct WriteHandle {
sender: mpsc::Sender<WriteMessage>,
}
impl WriteHandle {
pub async fn write(&self, stanza: Stanza) -> Result<(), WriteError> {
let (send, recv) = oneshot::channel();
self.send(WriteMessage {
stanza,
respond_to: send,
})
.await
.map_err(|e| WriteError::Actor(e.into()))?;
// TODO: timeout
recv.await.map_err(|e| WriteError::Actor(e.into()))?
}
}
impl Deref for WriteHandle {
type Target = mpsc::Sender<WriteMessage>;
fn deref(&self) -> &Self::Target {
&self.sender
}
}
impl DerefMut for WriteHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.sender
}
}
pub struct WriteControlHandle {
sender: mpsc::Sender<WriteControl>,
pub(crate) handle: JoinHandle<()>,
}
impl Deref for WriteControlHandle {
type Target = mpsc::Sender<WriteControl>;
fn deref(&self) -> &Self::Target {
&self.sender
}
}
impl DerefMut for WriteControlHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.sender
}
}
impl WriteControlHandle {
pub fn new(
stream: BoundJabberWriter<Tls>,
on_crash: oneshot::Sender<(WriteMessage, WriteState)>,
) -> (WriteHandle, Self) {
let (control_sender, control_receiver) = mpsc::channel(20);
let (stanza_sender, stanza_receiver) = mpsc::channel(20);
let actor = Write::new(stream, stanza_receiver, control_receiver, on_crash);
let handle = tokio::spawn(async move { actor.run().await });
(
WriteHandle {
sender: stanza_sender,
},
Self {
sender: control_sender,
handle,
},
)
}
pub fn reconnect_retry(
stream: BoundJabberWriter<Tls>,
on_crash: oneshot::Sender<(WriteMessage, WriteState)>,
stanza_receiver: mpsc::Receiver<WriteMessage>,
retry_msg: WriteMessage,
) -> Self {
let (control_sender, control_receiver) = mpsc::channel(20);
let actor = Write::new(stream, stanza_receiver, control_receiver, on_crash);
let handle = tokio::spawn(async move { actor.run_reconnected(retry_msg).await });
Self {
sender: control_sender,
handle,
}
}
pub fn reconnect(
stream: BoundJabberWriter<Tls>,
on_crash: oneshot::Sender<(WriteMessage, WriteState)>,
stanza_receiver: mpsc::Receiver<WriteMessage>,
) -> Self {
let (control_sender, control_receiver) = mpsc::channel(20);
let actor = Write::new(stream, stanza_receiver, control_receiver, on_crash);
let handle = tokio::spawn(async move { actor.run().await });
Self {
sender: control_sender,
handle,
}
}
}
|