actix_mqtt_client/actors/
mod.rs

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
use std::io;

#[macro_use]
pub mod macros;
pub mod actions;
pub mod packets;
mod utils;

use std::io::ErrorKind;

use actix::dev::ToEnvelope;
use actix::prelude::SendError;
use actix::{Actor, Addr, Arbiter, Handler, MailboxError, Message, Recipient};
use log::trace;
use tokio::time::{sleep_until, Instant};

use crate::consts::RESEND_DELAY;

/// The actix message indicating that the client is about to stop
#[derive(Message)]
#[rtype(result = "()")]
pub struct StopMessage;

/// The actix message containing the error happens inside the client
#[derive(Message)]
#[rtype(result = "()")]
pub struct ErrorMessage(pub io::Error);

pub fn send_error<T: AsRef<str>>(
    tag: &str,
    error_recipient: &Recipient<ErrorMessage>,
    kind: io::ErrorKind,
    message: T,
) {
    let error = io::Error::new(kind, message.as_ref());
    let send_result = error_recipient.try_send(ErrorMessage(error));
    log::debug!(
        "[{}] Send result for error recipient: {:?}",
        tag,
        send_result
    );
}

fn resend<TActor, TMessage>(addr: Addr<TActor>, msg: TMessage)
where
    TMessage: Message + Send + 'static,
    TActor: Actor + Handler<TMessage>,
    TMessage::Result: Send,
    TActor::Context: ToEnvelope<TActor, TMessage>,
{
    trace!("Schedule resend message");
    let later_func = async move {
        let delay_time = Instant::now() + RESEND_DELAY.clone();
        sleep_until(delay_time).await;
        addr.do_send(msg);
    };
    Arbiter::current().spawn(later_func);
}

fn handle_send_error<T>(
    tag: &str,
    e: SendError<T>,
    error_recipient: &Recipient<ErrorMessage>,
    stop_recipient: &Recipient<StopMessage>,
) {
    match e {
        SendError::Closed(_) => {
            send_error(
                tag,
                error_recipient,
                ErrorKind::Interrupted,
                format!("[{}] Target mailbox is closed", tag),
            );
            let _ = stop_recipient.do_send(StopMessage);
        }
        SendError::Full(_) => {
            // Do nothing
            send_error(
                tag,
                error_recipient,
                ErrorKind::Other,
                format!("[{}] Target mailbox is full", tag),
            );
        }
    }
}

fn handle_send_error_with_resend<T, TActor, TMessage>(
    tag: &str,
    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(
                tag,
                error_recipient,
                ErrorKind::Interrupted,
                format!("[{}] Target mailbox is closed", tag),
            );
            let _ = stop_recipient.do_send(StopMessage);
        }
        SendError::Full(_) => {
            send_error(
                tag,
                error_recipient,
                ErrorKind::Other,
                format!("[{}] Target mailbox is full, will retry send", tag),
            );
            resend(addr, msg);
        }
    }
}

// fn handle_mailbox_error(
//     e: MailboxError,
//     error_recipient: &Recipient<ErrorMessage>,
//     stop_recipient: &Recipient<StopMessage>,
// ) {
//     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 => {
//             // Do nothing
//             send_error(error_recipient, ErrorKind::Other, "Send timeout");
//         }
//     }
// }

fn handle_mailbox_error_with_resend<TActor, TMessage>(
    tag: &str,
    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(
                tag,
                error_recipient,
                ErrorKind::Interrupted,
                "Target mailbox is closed",
            );
            let _ = stop_recipient.do_send(StopMessage);
        }
        MailboxError::Timeout => {
            send_error(
                tag,
                error_recipient,
                ErrorKind::Other,
                "Send timeout, will resend",
            );

            resend(addr, msg);
        }
    }
}