saluki_core/components/forwarders/
context.rs

1use crate::accounting::ComponentRegistry;
2use crate::health::Health;
3use crate::runtime::SupervisorHandle;
4use crate::{
5    components::ComponentContext,
6    topology::{PayloadsConsumer, TopologyContext},
7};
8
9/// Forwarder context.
10pub struct ForwarderContext {
11    topology_context: TopologyContext,
12    component_context: ComponentContext,
13    component_registry: ComponentRegistry,
14    health_handle: Option<Health>,
15    consumer: PayloadsConsumer,
16    supervisor_handle: SupervisorHandle,
17}
18
19impl ForwarderContext {
20    /// Creates a new `ForwarderContext`.
21    pub fn new(
22        topology_context: &TopologyContext, component_context: &ComponentContext,
23        component_registry: ComponentRegistry, health_handle: Health, consumer: PayloadsConsumer,
24        supervisor_handle: SupervisorHandle,
25    ) -> Self {
26        Self {
27            topology_context: topology_context.clone(),
28            component_context: component_context.clone(),
29            component_registry,
30            health_handle: Some(health_handle),
31            consumer,
32            supervisor_handle,
33        }
34    }
35
36    /// Consumes the health handle of this forwarder context.
37    ///
38    /// # Panics
39    ///
40    /// Panics if the health handle has already been taken.
41    pub fn take_health_handle(&mut self) -> Health {
42        self.health_handle.take().expect("health handle already taken")
43    }
44
45    /// Gets a reference to the topology context.
46    pub fn topology_context(&self) -> &TopologyContext {
47        &self.topology_context
48    }
49
50    /// Gets a reference to the component context.
51    pub fn component_context(&self) -> &ComponentContext {
52        &self.component_context
53    }
54
55    /// Gets a reference to the component registry.
56    pub fn component_registry(&mut self) -> &ComponentRegistry {
57        &self.component_registry
58    }
59
60    /// Gets a mutable reference to the payloads consumer.
61    pub fn payloads(&mut self) -> &mut PayloadsConsumer {
62        &mut self.consumer
63    }
64
65    /// Returns a handle to the supervisor that this component is spawned on.
66    ///
67    /// Dynamic child processes can be spawned via the supervisor handle and thus have their lifecycle
68    /// coupled to the component itself: if the component restarts, or the component's supervisor dies,
69    /// the dynamic child processes will also be terminated automatically as well.
70    pub fn spawn_handle(&self) -> &SupervisorHandle {
71        &self.supervisor_handle
72    }
73}