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
//! Global Plexus RPC context for sharing state with activations
//!
//! This module provides a thread-safe way to share the Plexus RPC server hash
//! with all activations. The hash is set once during hub initialization
//! and can be read by any activation when creating stream items.
use std::sync::RwLock;
/// Global Plexus RPC context: the hash set once at the service's start
/// (NEST-15: and moved when that service's root changes while it serves).
static PLEXUS_CONTEXT: RwLock<Option<String>> = RwLock::new(None);
/// Shared Plexus RPC context accessible to all activations
#[derive(Debug, Clone)]
pub struct PlexusContext {
/// Hash of all activations for cache invalidation
pub hash: String,
}
impl PlexusContext {
/// Initialize the global Plexus RPC context
///
/// This should be called once during hub initialization.
/// Subsequent calls will be ignored.
pub fn init(hash: String) {
let mut ctx = PLEXUS_CONTEXT.write().unwrap_or_else(|p| p.into_inner());
if ctx.is_none() {
*ctx = Some(hash);
}
}
/// NEST-15: the service whose hash was `from` now serves `to` (its
/// root's table changed): items carry `to` from now on. A hash another
/// service set is left as it is.
pub(crate) fn moved(from: &str, to: &str) {
let mut ctx = PLEXUS_CONTEXT.write().unwrap_or_else(|p| p.into_inner());
if ctx.as_deref() == Some(from) {
*ctx = Some(to.to_string());
}
}
/// Get the global Plexus RPC server hash
///
/// Returns an empty string if the context hasn't been initialized yet.
pub fn hash() -> String {
PLEXUS_CONTEXT.read().unwrap_or_else(|p| p.into_inner()).clone().unwrap_or_default()
}
}