Skip to main content

middleware_core/
config.rs

1use std::time::Duration;
2
3use crate::{QosProfile, RouteRule};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum ReplayDegradeStrategy {
7    BlockPublisher,
8    DropOldest,
9}
10
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub struct TopicReliabilityPolicy {
13    pub max_retry: u8,
14    pub retry_timeout: Duration,
15    pub max_inflight_per_subscriber: usize,
16    pub replay_window: Option<usize>,
17    pub replay_degrade_strategy: ReplayDegradeStrategy,
18}
19
20impl Default for TopicReliabilityPolicy {
21    fn default() -> Self {
22        Self {
23            max_retry: 3,
24            retry_timeout: Duration::from_millis(200),
25            max_inflight_per_subscriber: 16,
26            replay_window: None,
27            replay_degrade_strategy: ReplayDegradeStrategy::BlockPublisher,
28        }
29    }
30}
31
32impl TopicReliabilityPolicy {
33    pub fn with_max_retry(mut self, max_retry: u8) -> Self {
34        self.max_retry = max_retry;
35        self
36    }
37
38    pub fn with_retry_timeout(mut self, retry_timeout: Duration) -> Self {
39        self.retry_timeout = retry_timeout;
40        self
41    }
42
43    pub fn with_max_inflight_per_subscriber(mut self, max_inflight: usize) -> Self {
44        self.max_inflight_per_subscriber = max_inflight;
45        self
46    }
47
48    pub fn with_replay_window(mut self, replay_window: Option<usize>) -> Self {
49        self.replay_window = replay_window;
50        self
51    }
52
53    pub fn with_replay_degrade_strategy(mut self, strategy: ReplayDegradeStrategy) -> Self {
54        self.replay_degrade_strategy = strategy;
55        self
56    }
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
60pub struct TopicQosOverride {
61    pub topic: String,
62    pub profile: QosProfile,
63}
64
65impl TopicQosOverride {
66    pub fn new(topic: impl Into<String>, profile: QosProfile) -> Self {
67        Self {
68            topic: topic.into(),
69            profile,
70        }
71    }
72}
73
74#[derive(Clone, Debug, Default, Eq, PartialEq)]
75pub struct MiddlewareRuntimeConfig {
76    pub route_rules: Vec<RouteRule>,
77    pub topic_qos_overrides: Vec<TopicQosOverride>,
78    pub namespace_isolation: bool,
79    pub topic_reliability_policy: TopicReliabilityPolicy,
80}
81
82impl MiddlewareRuntimeConfig {
83    pub fn with_namespace_isolation(mut self, enabled: bool) -> Self {
84        self.namespace_isolation = enabled;
85        self
86    }
87
88    pub fn add_route_rule(mut self, rule: RouteRule) -> Self {
89        self.route_rules.push(rule);
90        self
91    }
92
93    pub fn add_topic_qos_override(mut self, topic: impl Into<String>, profile: QosProfile) -> Self {
94        self.topic_qos_overrides
95            .push(TopicQosOverride::new(topic, profile));
96        self
97    }
98
99    pub fn with_topic_reliability_policy(mut self, policy: TopicReliabilityPolicy) -> Self {
100        self.topic_reliability_policy = policy;
101        self
102    }
103}