Skip to main content

saluki_core/components/relays/
context.rs

1use std::sync::Arc;
2
3use resource_accounting::ComponentRegistry;
4use saluki_common::sync::shutdown::ShutdownHandle;
5
6use crate::health::Health;
7use crate::{
8    components::ComponentContext,
9    topology::{PayloadsDispatcher, TopologyContext},
10};
11
12struct RelayContextInner {
13    topology_context: TopologyContext,
14    component_context: ComponentContext,
15    component_registry: ComponentRegistry,
16    dispatcher: PayloadsDispatcher,
17}
18
19/// Relay context.
20pub struct RelayContext {
21    shutdown_handle: Option<ShutdownHandle>,
22    health_handle: Option<Health>,
23    inner: Arc<RelayContextInner>,
24}
25
26impl RelayContext {
27    /// Creates a new `RelayContext`.
28    pub fn new(
29        topology_context: &TopologyContext, component_context: &ComponentContext,
30        component_registry: ComponentRegistry, shutdown_handle: ShutdownHandle, health_handle: Health,
31        dispatcher: PayloadsDispatcher,
32    ) -> Self {
33        Self {
34            shutdown_handle: Some(shutdown_handle),
35            health_handle: Some(health_handle),
36            inner: Arc::new(RelayContextInner {
37                topology_context: topology_context.clone(),
38                component_context: component_context.clone(),
39                component_registry,
40                dispatcher,
41            }),
42        }
43    }
44
45    /// Consumes the shutdown handle of this relay context.
46    ///
47    /// # Panics
48    ///
49    /// Panics if the shutdown handle has already been taken.
50    pub fn take_shutdown_handle(&mut self) -> ShutdownHandle {
51        self.shutdown_handle.take().expect("shutdown handle already taken")
52    }
53
54    /// Consumes the health handle of this relay context.
55    ///
56    /// # Panics
57    ///
58    /// Panics if the health handle has already been taken.
59    pub fn take_health_handle(&mut self) -> Health {
60        self.health_handle.take().expect("health handle already taken")
61    }
62
63    /// Gets a reference to the topology context.
64    pub fn topology_context(&self) -> &TopologyContext {
65        &self.inner.topology_context
66    }
67
68    /// Gets a reference to the component context.
69    pub fn component_context(&self) -> &ComponentContext {
70        &self.inner.component_context
71    }
72
73    /// Gets a reference to the component registry.
74    pub fn component_registry(&self) -> &ComponentRegistry {
75        &self.inner.component_registry
76    }
77
78    /// Gets a reference to the payloads dispatcher.
79    pub fn dispatcher(&self) -> &PayloadsDispatcher {
80        &self.inner.dispatcher
81    }
82}
83
84impl Clone for RelayContext {
85    fn clone(&self) -> Self {
86        Self {
87            shutdown_handle: None,
88            health_handle: None,
89            inner: self.inner.clone(),
90        }
91    }
92}