-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
children.rs
157 lines (134 loc) · 4.26 KB
/
children.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
use crate::broadcast::{BastionMessage, Broadcast};
use crate::context::BastionContext;
use futures::future::CatchUnwind;
use futures::pending;
use futures::poll;
use futures::prelude::*;
use runtime::task::JoinHandle;
use std::any::Any;
use std::fmt::Debug;
use std::future::Future;
use std::panic::UnwindSafe;
use std::pin::Pin;
use std::task::Poll;
use uuid::Uuid;
pub trait Shell: objekt::Clone + Send + Sync + Any + 'static {}
impl<T> Shell for T where T: objekt::Clone + Send + Sync + Any + 'static {}
pub trait Message: Shell + Debug {
fn as_any(&self) -> &dyn Any;
}
impl<T> Message for T
where
T: Shell + Debug,
{
fn as_any(&self) -> &dyn Any {
self
}
}
pub trait Closure: Fn(BastionContext, Box<dyn Message>) -> Pin<Box<dyn Fut>> + Shell {}
impl<T> Closure for T where T: Fn(BastionContext, Box<dyn Message>) -> Pin<Box<dyn Fut>> + Shell {}
// TODO: Ok(T) & Err(E)
pub trait Fut: Future<Output = Result<(), ()>> + Send + UnwindSafe {}
impl<T> Fut for T where T: Future<Output = Result<(), ()>> + Send + UnwindSafe {}
pub(super) struct Children {
thunk: Box<dyn Closure>,
msg: Box<dyn Message>,
bcast: Broadcast,
redundancy: usize,
}
struct Child {
exec: CatchUnwind<Pin<Box<dyn Fut>>>,
bcast: Broadcast,
}
impl Children {
pub(super) fn new(
thunk: Box<dyn Closure>,
msg: Box<dyn Message>,
bcast: Broadcast,
redundancy: usize,
) -> Self {
Children {
thunk,
msg,
bcast,
redundancy,
}
}
pub(super) fn id(&self) -> &Uuid {
self.bcast.id()
}
async fn run(mut self) -> Self {
loop {
match poll!(&mut self.bcast.next()) {
Poll::Ready(Some(msg)) => {
match msg {
BastionMessage::PoisonPill | BastionMessage::Dead { .. } | BastionMessage::Faulted { .. } => {
if msg.is_faulted() {
self.bcast.faulted();
} else {
self.bcast.dead();
}
return self;
}
// FIXME
BastionMessage::Message(_) => unimplemented!(),
}
}
Poll::Ready(None) => {
self.bcast.faulted();
return self;
}
Poll::Pending => pending!(),
}
}
}
pub(super) fn launch(mut self) -> JoinHandle<Self> {
for _ in 0..self.redundancy {
let id = Uuid::new_v4();
let bcast = self.bcast.new_child(id.clone());
let thunk = objekt::clone_box(&*self.thunk);
let msg = objekt::clone_box(&*self.msg);
let parent = self.bcast.sender().clone();
let ctx = BastionContext::new(id, parent);
let exec = thunk(ctx, msg)
.catch_unwind();
let child = Child { exec, bcast };
runtime::spawn(child.run());
}
runtime::spawn(self.run())
}
}
impl Child {
async fn run(mut self) {
loop {
if let Poll::Ready(res) = poll!(&mut self.exec) {
match res {
Ok(Ok(())) => self.bcast.dead(),
Ok(Err(())) | Err(_) => self.bcast.faulted(),
}
return;
}
match poll!(&mut self.bcast.next()) {
Poll::Ready(Some(msg)) => {
match msg {
BastionMessage::PoisonPill => {
self.bcast.dead();
return;
}
BastionMessage::Dead { .. } | BastionMessage::Faulted { .. } => {
self.bcast.faulted();
return;
}
// FIXME
BastionMessage::Message(_) => unimplemented!(),
}
}
Poll::Ready(None) => {
self.bcast.faulted();
return;
}
Poll::Pending => pending!(),
}
}
}
}