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
//! NEST-11 · which service a call is routed in.
//!
//! A call enters a service at [`DynamicHub::route_with_ctx`] (a socket's
//! `{ns}.call`, a [`HubTransport`](super::HubTransport), a test's `route`);
//! the outermost one it enters is its service, and everything that routing
//! reaches — an activation, a child mounted below it, a peer in a slot —
//! runs knowing it. A [`RemoteChild`](super::RemoteChild) keeps it, weakly,
//! for each call it forwards: the far side's calls back under that call are
//! routed through this service's own routing and gate, as its caller.
//!
//! Nothing here admits anyone: the service is a route, never a caller.
use std::future::Future;
use std::sync::Weak;
use super::plexus::DynamicHub;
tokio::task_local! {
static SERVICE: WeakService;
}
/// A service, held weakly: what a child keeps of the service it is mounted
/// in, so it never keeps that service alive. Cheap to clone.
#[derive(Clone)]
pub struct WeakService(Weak<super::plexus::DynamicHubInner>);
impl std::fmt::Debug for WeakService {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.upgrade() {
Some(s) => write!(f, "WeakService({})", s.runtime_namespace()),
None => f.write_str("WeakService(gone)"),
}
}
}
impl WeakService {
pub(crate) fn new(inner: Weak<super::plexus::DynamicHubInner>) -> Self {
Self(inner)
}
/// The service, while it lives.
pub fn upgrade(&self) -> Option<DynamicHub> {
self.0.upgrade().map(DynamicHub::from_inner)
}
}
/// Whether the running task is routing a call in some service already.
pub(crate) fn entered() -> bool {
SERVICE.try_with(|_| ()).is_ok()
}
/// The service the running call was routed in, if any.
pub(crate) fn current() -> Option<WeakService> {
SERVICE.try_with(WeakService::clone).ok()
}
/// Run `fut` routed in `service`.
pub(crate) fn scope<F: Future>(service: WeakService, fut: F) -> impl Future<Output = F::Output> {
SERVICE.scope(service, fut)
}