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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::compat::watch;
use crate::forwarders::StreamForwarder;
use crate::handlers::{
Consumer, Eliminated, Envelope, Interaction, InteractionDone, InteractionTask, InterruptedBy,
Operation, Parcel, StartedBy, TaskEliminated,
};
use crate::ids::{Id, IdOf};
use crate::lifecycle::{Awake, Done, LifecycleNotifier, LifetimeTracker};
use crate::linkage::Address;
use crate::lite_runtime::{self, LiteTask, Tag, TaskAddress};
use anyhow::Error;
use async_trait::async_trait;
use futures::channel::mpsc;
use futures::stream::{pending, FusedStream};
use futures::{select_biased, Stream, StreamExt};
use std::hash::Hash;
use thiserror::Error;
use uuid::Uuid;
#[derive(Debug, Error)]
enum Reason {
#[error("Actor is terminating...")]
Terminating,
}
const MESSAGES_CHANNEL_DEPTH: usize = 32;
#[async_trait]
pub trait Actor: Sized + Send + 'static {
type GroupBy: Clone + Send + Eq + Hash;
fn name(&self) -> String {
let uuid = Uuid::new_v4();
format!("Actor:{}({})", std::any::type_name::<Self>(), uuid)
}
#[doc(hidden)]
async fn queue_drained(&mut self, _ctx: &mut Context<Self>) -> Result<(), Error> {
Ok(())
}
#[doc(hidden)]
async fn instant_queue_drained(&mut self, _ctx: &mut Context<Self>) -> Result<(), Error> {
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Status {
Alive,
Stop,
}
impl Status {
pub fn is_done(&self) -> bool {
*self == Status::Stop
}
}
pub(crate) fn spawn<A, S>(
actor: A,
supervisor: Option<Address<S>>,
custom_name: Option<String>,
) -> Address<A>
where
A: Actor + StartedBy<S>,
S: Actor + Eliminated<A>,
{
let actor_name = custom_name.unwrap_or_else(|| actor.name());
let id = Id::new(actor_name);
let (hp_msg_tx, hp_msg_rx) = mpsc::unbounded();
let (msg_tx, msg_rx) = mpsc::channel(MESSAGES_CHANNEL_DEPTH);
let (join_tx, join_rx) = watch::channel(Status::Alive);
let address = Address::new(id, hp_msg_tx, msg_tx, join_rx);
let id: Id = address.id().into();
let awake_envelope = Envelope::instant(Awake::new());
let done_notifier = {
match supervisor {
None => <dyn LifecycleNotifier<_>>::ignore(),
Some(super_addr) => {
let op = Operation::Done { id };
<dyn LifecycleNotifier<_>>::once(super_addr, op)
}
}
};
let context = Context {
alive: true,
address: address.clone(),
lifetime_tracker: LifetimeTracker::new(),
};
let runtime = ActorRuntime {
id: address.id(),
actor,
context,
awake_envelope: Some(awake_envelope),
done_notifier,
msg_rx,
hp_msg_rx,
join_tx,
};
crate::compat::spawn_async(runtime.entrypoint());
address
}
pub struct Context<A: Actor> {
alive: bool,
address: Address<A>,
lifetime_tracker: LifetimeTracker<A>,
}
impl<A: Actor> Context<A> {
pub fn address(&mut self) -> &mut Address<A> {
&mut self.address
}
pub fn spawn_actor<T>(&mut self, actor: T, group: A::GroupBy) -> Address<T>
where
T: Actor + StartedBy<A> + InterruptedBy<A>,
A: Eliminated<T>,
{
let custom_name = None;
let address = spawn(actor, Some(self.address.clone()), custom_name);
self.lifetime_tracker.insert(address.clone(), group);
address
}
pub fn spawn_task<T, M>(&mut self, task: T, tag: M, group: A::GroupBy) -> TaskAddress<T>
where
T: LiteTask,
A: TaskEliminated<T, M>,
M: Tag,
{
let custom_name = None;
let stopper = lite_runtime::spawn(task, tag, Some(self.address.clone()), custom_name);
self.lifetime_tracker.insert_task(stopper.clone(), group);
stopper
}
pub fn attach<S, M>(&mut self, stream: S, tag: M, group: A::GroupBy)
where
S: Stream + Unpin + Send + 'static,
S::Item: Send,
A: Consumer<S::Item>,
M: Tag,
{
let forwarder = StreamForwarder::new(stream, self.address.clone());
self.spawn_task(forwarder, tag, group);
}
pub fn track_interaction<I, M>(&mut self, task: InteractionTask<I>, tag: M, group: A::GroupBy)
where
I: Interaction,
A: InteractionDone<I, M>,
M: Tag,
{
self.spawn_task(task, tag, group);
}
pub fn interrupt<T>(&mut self, address: &mut Address<T>) -> Result<(), Error>
where
T: Actor + InterruptedBy<A>,
{
address.interrupt_by()
}
pub fn not_terminating(&self) -> Result<(), Error> {
if self.is_terminating() {
Err(Reason::Terminating.into())
} else {
Ok(())
}
}
pub fn stop(&mut self) {
self.alive = false;
}
pub fn shutdown(&mut self) {
self.lifetime_tracker.start_termination();
if self.lifetime_tracker.is_finished() {
self.stop();
}
}
pub fn terminate_group(&mut self, group: A::GroupBy) {
self.lifetime_tracker.terminate_group(group)
}
pub fn is_terminating(&self) -> bool {
self.lifetime_tracker.is_terminating()
}
pub fn termination_sequence(&mut self, sequence: Vec<A::GroupBy>) {
self.lifetime_tracker.termination_sequence(sequence);
}
}
pub struct ActorRuntime<A: Actor> {
id: IdOf<A>,
actor: A,
context: Context<A>,
awake_envelope: Option<Envelope<A>>,
done_notifier: Box<dyn LifecycleNotifier<Done<A>>>,
msg_rx: mpsc::Receiver<Envelope<A>>,
hp_msg_rx: mpsc::UnboundedReceiver<Parcel<A>>,
join_tx: watch::Sender<Status>,
}
impl<A: Actor> ActorRuntime<A> {
async fn entrypoint(mut self) {
log::info!("Actor started: {:?}", self.id);
let mut awake_envelope = self
.awake_envelope
.take()
.expect("awake envelope has to be set in spawn method!");
let awake_res = awake_envelope
.handle(&mut self.actor, &mut self.context)
.await;
match awake_res {
Ok(_) => {
self.routine().await;
}
Err(err) => {
log::error!(
"Can't call awake notification handler of the actor {:?}: {}",
self.id,
err
);
}
}
log::info!("Actor finished: {:?}", self.id);
let done_event = Done::new(self.id.clone());
if let Err(err) = self.done_notifier.notify(done_event) {
log::error!(
"Can't send done notification from the actor {:?}: {}",
self.id,
err
);
}
if !self.join_tx.is_closed() {
if let Err(_err) = self.join_tx.send(Status::Stop) {
log::error!("Can't release joiners of {:?}", self.id);
}
}
}
async fn routine(&mut self) {
let mut scheduled_queue = crate::compat::DelayQueue::<Envelope<A>>::new().fuse();
let mut pendel = pending();
while self.context.alive {
let maybe_queue: &mut (dyn FusedStream<Item = Result<_, _>> + Unpin + Send) =
if scheduled_queue.get_ref().len() > 0 {
&mut scheduled_queue
} else {
&mut pendel
};
select_biased! {
hp_envelope = self.hp_msg_rx.next() => {
if let Some(hp_env) = hp_envelope {
let envelope = hp_env.envelope;
let process_envelope;
match hp_env.operation {
Operation::Forward => {
process_envelope = Some(envelope);
}
Operation::Done { id } => {
self.context.lifetime_tracker.remove(&id);
if self.context.lifetime_tracker.is_finished() {
self.context.stop();
}
process_envelope = Some(envelope);
}
Operation::Schedule { deadline } => {
scheduled_queue.get_mut().insert_at(envelope, deadline);
log::trace!("Scheduled events: {}", scheduled_queue.get_ref().len());
process_envelope = None;
}
}
if let Some(mut envelope) = process_envelope {
let handle_res = envelope.handle(&mut self.actor, &mut self.context).await;
if let Err(err) = handle_res {
log::error!("Handler for {:?} (high-priority) failed: {}", self.id, err);
}
}
} else {
log::trace!("Messages stream of {:?} (high-priority) drained.", self.id);
if let Err(err) = self.actor.instant_queue_drained(&mut self.context).await {
log::error!("Queue (high-priority) drained handler {:?} failed: {}", self.id, err);
}
}
}
opt_delayed_envelope = maybe_queue.next() => {
if let Some(delayed_envelope) = opt_delayed_envelope {
match delayed_envelope {
Ok(expired) => {
log::trace!("Execute scheduled event. Remained: {}", scheduled_queue.get_ref().len());
let mut envelope = expired.into_inner();
let handle_res = envelope.handle(&mut self.actor, &mut self.context).await;
if let Err(err) = handle_res {
log::error!("Handler for {:?} (scheduled) failed: {}", self.id, err);
}
}
Err(err) => {
log::error!("Failed scheduled execution for {:?}: {}", self.id, err);
}
}
} else {
log::error!("Delay queue of {} closed.", self.id);
if let Err(err) = self.actor.instant_queue_drained(&mut self.context).await {
log::error!("Queue (high-priority) drained handler {:?} failed: {}", self.id, err);
}
}
}
lp_envelope = self.msg_rx.next() => {
if let Some(mut envelope) = lp_envelope {
let handle_res = envelope.handle(&mut self.actor, &mut self.context).await;
if let Err(err) = handle_res {
log::error!("Handler for {:?} failed: {}", self.id, err);
}
} else {
log::trace!("Messages stream of {:?} drained.", self.id);
if let Err(err) = self.actor.queue_drained(&mut self.context).await {
log::error!("Queue drained handler {:?} failed: {}", self.id, err);
}
}
}
}
}
}
}