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
pub(crate) mod handler;
pub(crate) mod runner;
pub(crate) mod supervision;
use async_trait::async_trait;
use thiserror::Error;
mod path;
pub use path::ActorPath;
use supervision::SupervisionStrategy;
use crate::system::{ActorSystem, SystemEvent};
pub struct ActorContext<E: SystemEvent> {
pub path: ActorPath,
pub system: ActorSystem<E>,
}
impl<E: SystemEvent> ActorContext<E> {
pub async fn create_child<A: Actor<E>>(
&self,
name: &str,
actor: A,
) -> Result<ActorRef<E, A>, ActorError> {
let path = self.path.clone() / name;
self.system.create_actor_path(path, actor).await
}
pub async fn get_child<A: Actor<E>>(&self, name: &str) -> Option<ActorRef<E, A>> {
let path = self.path.clone() / name;
self.system.get_actor(&path).await
}
pub async fn get_or_create_child<A, F>(
&self,
name: &str,
actor_fn: F,
) -> Result<ActorRef<E, A>, ActorError>
where
A: Actor<E>,
F: FnOnce() -> A,
{
let path = self.path.clone() / name;
self.system.get_or_create_actor_path(&path, actor_fn).await
}
pub async fn stop_child(&self, name: &str) {
let path = self.path.clone() / name;
self.system.stop_actor(&path).await;
}
pub(crate) async fn restart<A>(
&mut self,
actor: &mut A,
error: Option<&ActorError>,
) -> Result<(), ActorError>
where
A: Actor<E>,
{
actor.pre_restart(self, error).await
}
}
pub trait Message: Clone + Send + Sync + 'static {
type Response: Send + Sync + 'static;
}
#[async_trait]
pub trait Handler<E: SystemEvent, M: Message>: Send + Sync {
async fn handle(&mut self, msg: M, ctx: &mut ActorContext<E>) -> M::Response;
}
#[async_trait]
pub trait Actor<E: SystemEvent>: Send + Sync + 'static {
fn supervision_strategy() -> SupervisionStrategy {
SupervisionStrategy::Stop
}
async fn pre_start(&mut self, _ctx: &mut ActorContext<E>) -> Result<(), ActorError> {
Ok(())
}
async fn pre_restart(
&mut self,
ctx: &mut ActorContext<E>,
_error: Option<&ActorError>,
) -> Result<(), ActorError> {
self.pre_start(ctx).await
}
async fn post_stop(&mut self, _ctx: &mut ActorContext<E>) {}
}
pub struct ActorRef<E: SystemEvent, A: Actor<E>> {
path: ActorPath,
sender: handler::HandlerRef<E, A>,
}
impl<E: SystemEvent, A: Actor<E>> Clone for ActorRef<E, A> {
fn clone(&self) -> Self {
Self { path: self.path.clone(), sender: self.sender.clone() }
}
}
impl<E: SystemEvent, A: Actor<E>> ActorRef<E, A> {
pub fn path(&self) -> &ActorPath {
&self.path
}
#[deprecated(since = "0.2.3", note = "please use `path` instead")]
pub fn get_path(&self) -> &ActorPath {
&self.path
}
pub fn tell<M>(&self, msg: M) -> Result<(), ActorError>
where
M: Message,
A: Handler<E, M>,
{
self.sender.tell(msg)
}
pub async fn ask<M>(&self, msg: M) -> Result<M::Response, ActorError>
where
M: Message,
A: Handler<E, M>,
{
self.sender.ask(msg).await
}
pub fn is_closed(&self) -> bool {
self.sender.is_closed()
}
pub(crate) fn new(path: ActorPath, sender: handler::MailboxSender<E, A>) -> Self {
let handler = handler::HandlerRef::new(sender);
ActorRef {
path,
sender: handler,
}
}
}
impl<E: SystemEvent, A: Actor<E>> std::fmt::Debug for ActorRef<E, A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path)
}
}
#[derive(Error, Debug)]
pub enum ActorError {
#[error("Actor exists")]
Exists(ActorPath),
#[error("Actor creation failed")]
CreateError(String),
#[error("Sending message failed")]
SendError(String),
#[error("Actor runtime error")]
RuntimeError(anyhow::Error),
}
impl ActorError {
pub fn new<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::RuntimeError(anyhow::Error::new(error))
}
}