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
/* ***
*
* Harmos
*
* SPDX-License-Identifier: Apache-2.0
*
* ***
*/
/* === Modules === */
/* === Re-Exports === */
/* === Imports === */
// Standard Library Imports
// Third Party Imports
// Local Imports
use super::*;
/* === Constants === */
/* === Macros === */
/* === Type Aliases === */
/* === Structs === */
/* === Enums === */
/* === Traits === */
/* === Implementations === */
impl<A, H> Exchange<'_, A, H>
where
A: Application,
H: Fn(&ServiceContext<A>, Notice) + Send + Sync,
{
pub(super) async fn absorb_frame(&mut self, arrival: Arrival) -> Absorbed {
match arrival {
Arrival::Binary(_) => unreachable!("binary ownership is consumed by absorb"),
Arrival::Publications(receipts) => {
self.deliveries.extend(receipts.into_iter().map(|receipt| Box::pin(receipt.committed()) as _));
Absorbed::Carried
}
Arrival::ObserverFailed(error) => Absorbed::Malformed(error),
Arrival::Demanded { stream, wanted } => {
self.demand.send_modify(|demand| {
demand.insert(stream, wanted);
});
Absorbed::Carried
}
Arrival::Frame(Frame::Answer { id, result }) => {
self.settle(id, Ok(result));
Absorbed::Carried
}
Arrival::Frame(Frame::Refusal { id, refusal }) => {
let refused = SidecarError::Refused {
code: refusal.code(),
failure: refusal.into_failure(),
};
self.settle(id, Err(refused));
Absorbed::Carried
}
Arrival::Frame(Frame::Push { method, params }) => {
if method == Frame::EXITING {
self.announced = Some(reason(¶ms));
} else {
return Absorbed::Malformed("application notifications require an admitted operation".to_owned());
}
Absorbed::Carried
}
Arrival::Frame(Frame::Call { id, method, params }) => {
if self.routed_tasks.contains_key(&id) {
return Absorbed::Malformed("duplicate live route call id".to_owned());
}
if method == crate::operations::CHILD_METHOD {
let submitted = executor::now();
let child = match crate::operations::WireChild::decode(¶ms) {
Ok(child) => child,
Err(error) => {
let refusal = harmos_domain::Refusal::of(error);
return self
.send(Frame::Refusal { id, refusal })
.await
.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried);
}
};
let scope = self.child_scopes.values().find(|scope| &scope.context == child.context()).cloned();
let admission = if self.routed_tasks.len() >= CHILD_REQUESTS {
Err(crate::plugin::DispatchError::Busy.into_refusal())
} else if let Some(scope) = scope {
Ok(Some(scope))
} else {
Ok(None)
};
let refusal = match admission {
Err(refusal) => refusal,
Ok(scope) => {
let (cancellation, cancelled) = oneshot::channel();
let invocation = match scope {
Some(scope) => Box::pin(async move {
match child {
crate::operations::WireChild::Call(call) => scope.call(call, submitted, cancelled).await,
crate::operations::WireChild::Control(control) => scope.control(control, cancelled).await,
}
})
as Pin<Box<dyn Future<Output = Result<Vec<u8>, harmos_domain::Refusal>> + Send>>,
None => {
// Submitted Children Belong To An Attempt Scope
let call = match child {
crate::operations::WireChild::Call(call) if !call.submit => call,
_ => {
let refusal = harmos_domain::Refusal::new(
harmos_domain::UNKNOWN,
"submitted children require an attempt context",
);
return self
.send(Frame::Refusal { id, refusal })
.await
.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried);
}
};
match if call.generation_context.as_ref().is_some_and(|token| {
self.plugin.as_ref().is_some_and(|scope| scope.accepts_generation_context(token))
}) {
self.caller.as_ref().ok_or(crate::Error::Closing { domain: "native generation caller" }).and_then(
|caller| {
caller.submit_encoded_at(
&call.operation,
call.fingerprint,
call.generation,
call.payload,
submitted,
call.values,
)
},
)
} else {
Err(crate::Error::Closing {
domain: "native attempt or generation context",
})
} {
Ok(invocation) => Box::pin(async move {
crate::operations::children::lifecycle(invocation, cancelled)
.await
.map_err(crate::operations::children::refused)
}),
Err(error) => {
let refusal = crate::operations::children::refused(error);
return self
.send(Frame::Refusal { id, refusal })
.await
.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried);
}
}
}
};
let returns = self.returns.clone();
let token = Arc::new(());
let task_token = Arc::clone(&token);
let future = async move {
let frame = match invocation.await {
Ok(result) => Frame::Answer { id, result: result.into() },
Err(error) => Frame::Refusal { id, refusal: error },
};
let _ = returns.send((frame, task_token)).await;
};
self.routed_tasks.insert(
id,
RoutedTask {
running: executor::task(future),
token,
cancellation: Some(cancellation),
failure: None,
},
);
return Absorbed::Carried;
}
};
return self
.send(Frame::Refusal { id, refusal })
.await
.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried);
}
let refusal = if self.routed_tasks.len() >= CHILD_REQUESTS {
crate::plugin::DispatchError::Busy.into_refusal()
} else if let Some(caller) = &self.caller {
match caller.submit_encoded(&method, params.into_vec()) {
Ok(invocation) => {
let returns = self.returns.clone();
let token = Arc::new(());
let task_token = Arc::clone(&token);
let (cancellation, cancelled) = oneshot::channel();
let future = async move {
let outcome = crate::operations::children::lifecycle(invocation, cancelled).await;
let frame = match outcome {
Ok(result) => Frame::Answer { id, result: result.into() },
Err(error) => {
let refusal = crate::operations::children::refused(error);
Frame::Refusal { id, refusal }
}
};
let _ = returns.send((frame, task_token)).await;
};
self.routed_tasks.insert(
id,
RoutedTask {
running: executor::task(future),
token,
cancellation: Some(cancellation),
failure: None,
},
);
return Absorbed::Carried;
}
Err(error) => crate::operations::children::refused(error),
}
} else {
harmos_domain::Refusal::new(-32_606, "no managed operation caller is configured")
};
self.send(Frame::Refusal { id, refusal })
.await
.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried)
}
Arrival::Frame(Frame::Cancel { id }) => {
if let Some(task) = self.routed_tasks.get_mut(&id)
&& let Some(cancel) = task.cancellation.take()
{
let _ = cancel.send(());
}
// Keep The Slot Until Its Original Running Physically Settles
Absorbed::Carried
}
Arrival::Reply(frame, token) => {
let id = match &frame {
Frame::Answer { id, .. } | Frame::Refusal { id, .. } => *id,
_ => unreachable!("routed tasks produce only replies"),
};
// A cancelled id can be reused before its already-queued reply
// is consumed. Only the task that owns the current slot may reply.
if !self.routed_tasks.get(&id).is_some_and(|current| Arc::ptr_eq(¤t.token, &token)) {
return Absorbed::Carried;
}
let task = self.routed_tasks.get_mut(&id).expect("the reply owns its slot");
if let Err(error) = (&mut task.running).await {
let failure = format!("native operation drain failed: {}", harmos_domain::failure::Report(&error));
task.failure = Some(failure.clone());
*self.cleanup_failure = Some(failure.clone());
return Absorbed::Malformed(failure);
}
self.routed_tasks.remove(&id);
self.send(frame).await.map_or_else(|error| Absorbed::Malformed(error.to_string()), |()| Absorbed::Carried)
}
Arrival::Diagnostic(line) => {
(self.notice)(self.context, Notice::Logged { line });
Absorbed::Carried
}
Arrival::Ended => Absorbed::Ended,
}
}
pub(super) fn deliver(&mut self, publication: ManagedPublication) -> Result<(), String> {
if let Some(ingest) = self.ingress.streams.get(publication.stream()) {
ingest(self.context, &publication)?;
return Ok(());
}
(self.notice)(self.context, Notice::Published { publication });
Ok(())
}
pub(super) fn settle(&mut self, id: u64, answer: Result<harmos_sidecar::FramePayload, SidecarError>) {
self.child_scopes.remove(&id);
if let Some(waiting) = self.pending.remove(&id) {
let _ = waiting.send(answer);
}
}
pub(super) fn mint(&mut self) -> u64 {
let id = self.next;
self.next += 1;
id
}
pub(super) async fn send(&mut self, frame: Frame) -> std::io::Result<()> {
self.outgoing.send(frame).await
}
}
/* === Public Functions === */
/* === Private Functions === */
/* === Tests === */