flare_core_runtime/
config.rs1use std::time::Duration;
6
7#[derive(Debug, Clone)]
11pub struct PollWorkerConfig {
12 pub concurrency: usize,
14 pub idle_backoff: Duration,
16 pub error_backoff: Duration,
18}
19
20impl Default for PollWorkerConfig {
21 fn default() -> Self {
22 Self {
23 concurrency: 4,
24 idle_backoff: Duration::from_millis(100),
25 error_backoff: Duration::from_secs(1),
26 }
27 }
28}
29
30impl PollWorkerConfig {
31 pub fn new() -> Self {
33 Self::default()
34 }
35
36 pub fn with_concurrency(mut self, concurrency: usize) -> Self {
38 self.concurrency = concurrency;
39 self
40 }
41
42 pub fn with_idle_backoff(mut self, d: Duration) -> Self {
44 self.idle_backoff = d;
45 self
46 }
47
48 pub fn with_error_backoff(mut self, d: Duration) -> Self {
50 self.error_backoff = d;
51 self
52 }
53}
54
55#[derive(Debug, Clone)]
57pub struct TaskStartupConfig {
58 pub concurrency: usize,
60 pub timeout: Duration,
62 pub enable_ready_check: bool,
64 pub ready_check_timeout: Duration,
66}
67
68impl Default for TaskStartupConfig {
69 fn default() -> Self {
70 Self {
71 concurrency: num_cpus::get(),
72 timeout: Duration::from_secs(30),
73 enable_ready_check: true,
74 ready_check_timeout: Duration::from_secs(30),
75 }
76 }
77}
78
79impl TaskStartupConfig {
80 pub fn new() -> Self {
82 Self::default()
83 }
84
85 pub fn with_concurrency(mut self, concurrency: usize) -> Self {
87 self.concurrency = concurrency;
88 self
89 }
90
91 pub fn with_timeout(mut self, timeout: Duration) -> Self {
93 self.timeout = timeout;
94 self
95 }
96
97 pub fn with_ready_check(mut self, enable: bool) -> Self {
99 self.enable_ready_check = enable;
100 self
101 }
102
103 pub fn with_ready_check_timeout(mut self, timeout: Duration) -> Self {
105 self.ready_check_timeout = timeout;
106 self
107 }
108}
109
110#[derive(Debug, Clone)]
112pub struct HealthCheckConfig {
113 pub interval: Duration,
115 pub timeout: Duration,
117 pub failure_threshold: u32,
119 pub enabled: bool,
121}
122
123impl Default for HealthCheckConfig {
124 fn default() -> Self {
125 Self {
126 interval: Duration::from_secs(10),
127 timeout: Duration::from_secs(5),
128 failure_threshold: 3,
129 enabled: true,
130 }
131 }
132}
133
134impl HealthCheckConfig {
135 pub fn new() -> Self {
137 Self::default()
138 }
139
140 pub fn with_interval(mut self, interval: Duration) -> Self {
142 self.interval = interval;
143 self
144 }
145
146 pub fn with_timeout(mut self, timeout: Duration) -> Self {
148 self.timeout = timeout;
149 self
150 }
151
152 pub fn with_failure_threshold(mut self, threshold: u32) -> Self {
154 self.failure_threshold = threshold;
155 self
156 }
157
158 pub fn with_enabled(mut self, enabled: bool) -> Self {
160 self.enabled = enabled;
161 self
162 }
163}
164
165#[derive(Debug, Clone)]
167pub struct MetricsConfig {
168 pub enabled: bool,
170 pub port: u16,
172 pub path: String,
174}
175
176impl Default for MetricsConfig {
177 fn default() -> Self {
178 Self {
179 enabled: true,
180 port: 9090,
181 path: "/metrics".to_string(),
182 }
183 }
184}
185
186impl MetricsConfig {
187 pub fn new() -> Self {
189 Self::default()
190 }
191
192 pub fn with_enabled(mut self, enabled: bool) -> Self {
194 self.enabled = enabled;
195 self
196 }
197
198 pub fn with_port(mut self, port: u16) -> Self {
200 self.port = port;
201 self
202 }
203
204 pub fn with_path(mut self, path: impl Into<String>) -> Self {
206 self.path = path.into();
207 self
208 }
209}
210
211#[derive(Debug, Clone)]
213pub struct RuntimeConfig {
214 pub shutdown_timeout: Duration,
216 pub task_startup: TaskStartupConfig,
218 pub health_check: HealthCheckConfig,
220 pub metrics: MetricsConfig,
222 pub default_poll_worker: PollWorkerConfig,
224}
225
226impl Default for RuntimeConfig {
227 fn default() -> Self {
228 Self {
229 shutdown_timeout: Duration::from_secs(5),
230 task_startup: TaskStartupConfig::default(),
231 health_check: HealthCheckConfig::default(),
232 metrics: MetricsConfig::default(),
233 default_poll_worker: PollWorkerConfig::default(),
234 }
235 }
236}
237
238impl RuntimeConfig {
239 pub fn new() -> Self {
241 Self::default()
242 }
243
244 pub fn with_shutdown_timeout(mut self, timeout: Duration) -> Self {
246 self.shutdown_timeout = timeout;
247 self
248 }
249
250 pub fn with_task_startup(mut self, config: TaskStartupConfig) -> Self {
252 self.task_startup = config;
253 self
254 }
255
256 pub fn with_health_check(mut self, config: HealthCheckConfig) -> Self {
258 self.health_check = config;
259 self
260 }
261
262 pub fn with_metrics(mut self, config: MetricsConfig) -> Self {
264 self.metrics = config;
265 self
266 }
267
268 pub fn with_default_poll_worker(mut self, config: PollWorkerConfig) -> Self {
270 self.default_poll_worker = config;
271 self
272 }
273
274 pub fn validate(&self) -> Result<(), String> {
276 if self.shutdown_timeout.is_zero() {
277 return Err("shutdown_timeout must be greater than zero".to_string());
278 }
279
280 if self.task_startup.concurrency == 0 {
281 return Err("task_startup.concurrency must be greater than zero".to_string());
282 }
283
284 if self.health_check.failure_threshold == 0 {
285 return Err("health_check.failure_threshold must be greater than zero".to_string());
286 }
287
288 Ok(())
289 }
290}