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
use std::io;
#[macro_use]
pub mod macros;
pub mod actions;
pub mod packets;
mod utils;
use std::io::ErrorKind;
use std::thread::sleep;
use std::time::Instant;
use actix::dev::ToEnvelope;
use actix::prelude::SendError;
use actix::{Actor, Addr, Arbiter, Handler, MailboxError, Message, Recipient, System};
use futures::Future;
use log::{error, info};
use tokio::timer::Delay;
use crate::consts::{DELAY_BEFORE_SHUTDOWN, RESEND_DELAY};
use crate::errors;
#[derive(Message)]
pub struct StopMessage;
#[derive(Message)]
pub struct ErrorMessage(pub io::Error);
pub fn stop_system(stop_recipient: &Recipient<StopMessage>, code: i32) {
error!("Recipient is closed, stop the system");
let _ = stop_recipient.do_send(StopMessage);
sleep(DELAY_BEFORE_SHUTDOWN.clone());
System::current().stop_with_code(code);
}
pub fn force_stop_system(code: i32) {
error!("Recipient is closed, stop the system");
sleep(DELAY_BEFORE_SHUTDOWN.clone());
System::current().stop_with_code(code);
}
pub fn send_error<T: AsRef<str>>(
error_recipient: &Recipient<ErrorMessage>,
kind: io::ErrorKind,
message: T,
) {
let error = io::Error::new(kind, message.as_ref());
let _ = error_recipient.do_send(ErrorMessage(error));
}
fn resend<TActor, TMessage>(
addr: Addr<TActor>,
msg: TMessage,
error_recipient: Recipient<ErrorMessage>,
) where
TMessage: Message + Send + 'static,
TActor: Actor + Handler<TMessage>,
TMessage::Result: Send,
TActor::Context: ToEnvelope<TActor, TMessage>,
{
info!("Schedule resend message");
let delay_time = Instant::now() + RESEND_DELAY.clone();
let later_func = Delay::new(delay_time)
.map(move |_| {
addr.do_send(msg);
})
.map_err(move |e| {
send_error(
&error_recipient,
ErrorKind::TimedOut,
format!("Failed to delay: {}", e),
);
});
Arbiter::spawn(later_func);
}
fn handle_send_error<T>(
e: SendError<T>,
error_recipient: &Recipient<ErrorMessage>,
stop_recipient: &Recipient<StopMessage>,
) {
match e {
SendError::Closed(_) => {
send_error(
error_recipient,
ErrorKind::Interrupted,
"Target mailbox is closed",
);
stop_system(stop_recipient, errors::ERROR_CODE_FAILED_TO_SEND_MESSAGE);
}
SendError::Full(_) => {
send_error(error_recipient, ErrorKind::Other, "Target mailbox is full");
}
}
}
fn handle_send_error_with_resend<T, TActor, TMessage>(
e: SendError<T>,
error_recipient: &Recipient<ErrorMessage>,
stop_recipient: &Recipient<StopMessage>,
addr: Addr<TActor>,
msg: TMessage,
) where
TMessage: Message + Send + 'static,
TActor: Actor + Handler<TMessage>,
TMessage::Result: Send,
TActor::Context: ToEnvelope<TActor, TMessage>,
{
match e {
SendError::Closed(_) => {
send_error(
error_recipient,
ErrorKind::Interrupted,
"Target mailbox is closed",
);
stop_system(stop_recipient, errors::ERROR_CODE_FAILED_TO_SEND_MESSAGE);
}
SendError::Full(_) => {
send_error(
error_recipient,
ErrorKind::Other,
"Target mailbox is full, will retry send",
);
resend(addr, msg, error_recipient.clone());
}
}
}
fn handle_mailbox_error_with_resend<TActor, TMessage>(
e: MailboxError,
error_recipient: &Recipient<ErrorMessage>,
stop_recipient: &Recipient<StopMessage>,
addr: Addr<TActor>,
msg: TMessage,
) where
TMessage: Message + Send + 'static,
TActor: Actor + Handler<TMessage>,
TMessage::Result: Send,
TActor::Context: ToEnvelope<TActor, TMessage>,
{
match e {
MailboxError::Closed => {
send_error(
error_recipient,
ErrorKind::Interrupted,
"Target mailbox is closed",
);
stop_system(stop_recipient, errors::ERROR_CODE_FAILED_TO_SEND_MESSAGE);
}
MailboxError::Timeout => {
send_error(
error_recipient,
ErrorKind::Other,
"Send timeout, will resend",
);
resend(addr, msg, error_recipient.clone());
}
}
}