saluki_core/components/forwarders/
context.rs

1use crate::accounting::ComponentRegistry;
2use crate::health::Health;
3use crate::{
4    components::{ComponentContext, ComponentSpawner},
5    topology::{PayloadsConsumer, TopologyContext},
6};
7
8/// Forwarder context.
9pub struct ForwarderContext {
10    topology_context: TopologyContext,
11    component_context: ComponentContext,
12    component_registry: ComponentRegistry,
13    health_handle: Option<Health>,
14    consumer: PayloadsConsumer,
15    spawner: ComponentSpawner,
16}
17
18impl ForwarderContext {
19    /// Creates a new `ForwarderContext`.
20    pub fn new(
21        topology_context: &TopologyContext, component_context: &ComponentContext,
22        component_registry: ComponentRegistry, health_handle: Health, consumer: PayloadsConsumer,
23        spawner: ComponentSpawner,
24    ) -> Self {
25        Self {
26            topology_context: topology_context.clone(),
27            component_context: component_context.clone(),
28            component_registry,
29            health_handle: Some(health_handle),
30            consumer,
31            spawner,
32        }
33    }
34
35    /// Consumes the health handle of this forwarder context.
36    ///
37    /// # Panics
38    ///
39    /// Panics if the health handle has already been taken.
40    pub fn take_health_handle(&mut self) -> Health {
41        self.health_handle.take().expect("health handle already taken")
42    }
43
44    /// Returns a reference to the topology context.
45    pub fn topology_context(&self) -> &TopologyContext {
46        &self.topology_context
47    }
48
49    /// Returns a reference to the component context.
50    pub fn component_context(&self) -> &ComponentContext {
51        &self.component_context
52    }
53
54    /// Returns a reference to the component registry.
55    pub fn component_registry(&mut self) -> &ComponentRegistry {
56        &self.component_registry
57    }
58
59    /// Returns a mutable reference to the payloads consumer.
60    pub fn payloads(&mut self) -> &mut PayloadsConsumer {
61        &mut self.consumer
62    }
63
64    /// Returns a spawner for supervised child tasks belonging to this component.
65    ///
66    /// All child tasks spawned through this mechanism are tied to the lifecycle of the component itself, such that
67    /// they're automatically shutdown/stopped when the component is stopped during topology shutdown, etc.
68    pub fn spawner(&self) -> &ComponentSpawner {
69        &self.spawner
70    }
71}