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
use tracing::{debug, warn};
use crate::actor::{Actor, ActorContext, ActorId, ActorState, Stopping};
use crate::address::{Address, Mailbox, Recipient, SenderId};
use crate::channel::mpsc;
use crate::envelope::EnvelopeProxy;
use crate::supervisor::SupervisionEvent;
/// The default mailbox capacity for actors.
pub const DEFAULT_MAILBOX_CAPACITY: usize = 8;
/// The default implementation of an actor context.
#[derive(Debug)]
pub struct Context<A>
where
A: Actor<Context = Self>,
{
label: String,
state: ActorState,
doorplate: Address<A>,
mailbox: Option<Mailbox<A>>,
drain_mailbox: bool,
error: Option<A::Error>, // error happened in message handlers
supervisor: Option<Recipient<SupervisionEvent<A>>>,
}
impl<A> Context<A>
where
A: Actor<Context = Self>,
{
/// Constructs a new [`Context`] with a specific capacity.
pub fn with_capacity(label: String, capacity: usize) -> Self {
let (tx, rx) = mpsc::channel(capacity);
Self {
label,
state: ActorState::Unstarted,
doorplate: Address::new(tx),
mailbox: Some(Mailbox::new(rx)),
drain_mailbox: false,
supervisor: None,
error: None,
}
}
/// Saves an error in message handlers.
///
/// The actor will enter the [`Stopping`][ActorState::Stopping] state after processing
/// the current message.
pub fn save_error(&mut self, error: A::Error) {
self.error = Some(error);
}
/// Terminates the actor and saves the error.
///
/// The actor will enter the [`Stopped`][ActorState::Stopped] state after processing the
/// current message.
pub fn terminate_with_error(&mut self, error: A::Error) {
self.save_error(error);
self.terminate();
}
/// Schedules a one-time discard of messages already queued in the mailbox.
///
/// Sets a flag; the processing loop acts on it on its next iteration by snapshotting
/// `mailbox.len()` and discarding exactly that many messages. Messages enqueued after
/// the snapshot are delivered normally.
pub fn drain_mailbox(&mut self) {
self.drain_mailbox = true;
}
fn take_error(&mut self) -> Result<(), A::Error> {
match self.error.take() {
Some(e) => Err(e),
None => Ok(()),
}
}
}
impl<A> ActorContext<A> for Context<A>
where
A: Actor<Context = Self>,
{
fn new(label: String) -> Self {
Self::with_capacity(label, DEFAULT_MAILBOX_CAPACITY)
}
fn index(&self) -> ActorId {
self.doorplate.index()
}
fn label(&self) -> &str {
self.label.as_str()
}
fn address(&self) -> Address<A> {
self.doorplate.clone()
}
fn take_mailbox(&mut self) -> Option<Mailbox<A>> {
self.mailbox.take()
}
fn state(&self) -> ActorState {
self.state
}
fn set_state(&mut self, state: ActorState) {
self.state = state;
self.try_notify_supervisor(SupervisionEvent::State(self.address(), state));
}
async fn process_loop(
&mut self,
actor: &mut A,
mailbox: &mut Mailbox<A>,
) -> Result<(), A::Error> {
while self.state() == ActorState::Running {
if self.drain_mailbox {
let count = mailbox.len();
for _ in 0..count {
// the mailbox contains `count` messages, so try_recv never fail
let _ = mailbox.try_recv();
}
self.drain_mailbox = false;
}
match mailbox.recv().await {
Ok(mut envelope) => {
envelope.handle(actor, self).await;
if self.error.is_some() && self.state() == ActorState::Running {
self.set_state(ActorState::Stopping);
}
}
Err(_) => {
warn!("Mailbox is dropped, terminate the actor");
self.set_state(ActorState::Stopped);
}
};
match self.state() {
ActorState::Stopping => {
let result = self.take_error();
// if `stopping` returns `Err`, the actor will stop, if there is a saved error,
// the error is returned, otherwise the error from `stopping` is returned
match actor.stopping(self).await {
Ok(Stopping::Stop) => return result,
Ok(Stopping::Continue) => {
// resumed by the actor itself
if let Err(e) = result {
self.try_notify_supervisor(SupervisionEvent::Warn(
self.address(),
e,
))
};
self.set_state(ActorState::Running);
}
Err(e) => return result.or(Err(e)),
}
}
ActorState::Stopped => {
return self.take_error();
}
_ => {}
}
}
Ok(())
}
fn supervisor(&self) -> Option<&Recipient<SupervisionEvent<A>>> {
self.supervisor.as_ref()
}
fn set_supervisor(&mut self, supervisor: Option<Recipient<SupervisionEvent<A>>>) {
match supervisor {
Some(supervisor) => {
if supervisor.index() == self.index() {
warn!("Could not set the actor itself as its supervisor");
return;
}
debug!("Set actor {} as supervisor", supervisor.index());
self.supervisor = Some(supervisor);
}
None => {
if self.supervisor.take().is_some() {
debug!("Unset supervisor");
}
}
}
}
}