rust-tokio-supervisor 0.1.3

A Rust tokio supervisor with declarative task supervision, restart policy, shutdown coordination, and observability.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! YAML-friendly policy configuration models.
//!
//! This module owns configuration input structs for lower-level supervision
//! policy objects whose runtime form uses `Duration`, `ChildId`, or other
//! strongly typed values.

use confique::Config;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::time::Duration;

use crate::id::types::ChildId;
use crate::policy::budget as runtime_budget;
use crate::policy::failure_window as runtime_failure_window;
use crate::policy::group::{GroupDependencyEdge, PropagationPolicy};
use crate::policy::meltdown::MeltdownPolicy;
use crate::policy::task_role_defaults::{SeverityClass, TaskRole};
use crate::spec::supervisor::{
    ChildStrategyOverride, DynamicSupervisorPolicy, EscalationPolicy,
    GroupConfig as RuntimeGroupConfig, GroupStrategy, RestartLimit, SupervisionStrategy,
};

/// Restart budget configuration loaded from YAML.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Config, JsonSchema)]
pub struct RestartBudgetConfig {
    /// Sliding window duration in seconds.
    #[config(default = 60)]
    #[serde(default = "default_restart_budget_window_secs")]
    pub window_secs: u64,
    /// Maximum burst failures allowed within the window.
    #[config(default = 10)]
    #[serde(default = "default_restart_budget_max_burst")]
    pub max_burst: u32,
    /// Token recovery rate per second.
    #[config(default = 0.5)]
    #[serde(default = "default_restart_budget_recovery_rate")]
    pub recovery_rate_per_sec: f64,
}

impl RestartBudgetConfig {
    /// Converts this YAML-friendly config into the runtime restart budget.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`runtime_budget::RestartBudgetConfig`] value.
    pub fn to_runtime(&self) -> runtime_budget::RestartBudgetConfig {
        runtime_budget::RestartBudgetConfig::new(
            Duration::from_secs(self.window_secs),
            self.max_burst,
            self.recovery_rate_per_sec,
        )
    }
}

impl Default for RestartBudgetConfig {
    /// Returns the default restart budget configuration.
    fn default() -> Self {
        Self {
            window_secs: default_restart_budget_window_secs(),
            max_burst: default_restart_budget_max_burst(),
            recovery_rate_per_sec: default_restart_budget_recovery_rate(),
        }
    }
}

/// Failure window mode loaded from YAML.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum FailureWindowMode {
    /// Count failures that occur inside a time window.
    TimeSliding,
    /// Keep the most recent failure samples by count.
    CountSliding,
}

impl Default for FailureWindowMode {
    /// Returns the default time-sliding mode.
    fn default() -> Self {
        Self::TimeSliding
    }
}

/// Failure window configuration loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct FailureWindowConfig {
    /// Window mode selection.
    #[config(default = "time_sliding")]
    #[serde(default)]
    pub mode: FailureWindowMode,
    /// Time window width in seconds for `time_sliding` mode.
    #[config(default = 60)]
    #[serde(default = "default_failure_window_secs")]
    pub window_secs: u64,
    /// Maximum retained failure count for `count_sliding` mode.
    #[config(default = 5)]
    #[serde(default = "default_failure_window_max_count")]
    pub max_count: usize,
    /// Failure threshold at which the window is considered exhausted.
    #[config(default = 5)]
    #[serde(default = "default_failure_window_threshold")]
    pub threshold: usize,
}

impl FailureWindowConfig {
    /// Converts this YAML-friendly config into the runtime failure window.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`runtime_failure_window::FailureWindowConfig`] value.
    pub fn to_runtime(&self) -> runtime_failure_window::FailureWindowConfig {
        match self.mode {
            FailureWindowMode::TimeSliding => {
                runtime_failure_window::FailureWindowConfig::time_sliding(
                    self.window_secs,
                    self.threshold,
                )
            }
            FailureWindowMode::CountSliding => {
                runtime_failure_window::FailureWindowConfig::count_sliding(
                    self.max_count,
                    self.threshold,
                )
            }
        }
    }
}

impl Default for FailureWindowConfig {
    /// Returns the default failure window configuration.
    fn default() -> Self {
        Self {
            mode: FailureWindowMode::default(),
            window_secs: default_failure_window_secs(),
            max_count: default_failure_window_max_count(),
            threshold: default_failure_window_threshold(),
        }
    }
}

/// Meltdown fuse configuration loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct MeltdownConfig {
    /// Maximum restarts allowed for one child inside the child window.
    #[config(default = 3)]
    #[serde(default = "default_meltdown_child_max_restarts")]
    pub child_max_restarts: u32,
    /// Window used to count child restarts, in seconds.
    #[config(default = 10)]
    #[serde(default = "default_meltdown_child_window_secs")]
    pub child_window_secs: u64,
    /// Maximum failures allowed for one group inside the group window.
    #[config(default = 5)]
    #[serde(default = "default_meltdown_group_max_failures")]
    pub group_max_failures: u32,
    /// Window used to count group failures, in seconds.
    #[config(default = 30)]
    #[serde(default = "default_meltdown_group_window_secs")]
    pub group_window_secs: u64,
    /// Maximum failures allowed for the supervisor inside the supervisor window.
    #[config(default = 10)]
    #[serde(default = "default_meltdown_supervisor_max_failures")]
    pub supervisor_max_failures: u32,
    /// Window used to count supervisor failures, in seconds.
    #[config(default = 60)]
    #[serde(default = "default_meltdown_supervisor_window_secs")]
    pub supervisor_window_secs: u64,
    /// Stable duration after which recorded counters may be cleared, in seconds.
    #[config(default = 120)]
    #[serde(default = "default_meltdown_reset_after_secs")]
    pub reset_after_secs: u64,
}

impl MeltdownConfig {
    /// Converts this YAML-friendly config into the runtime meltdown policy.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`MeltdownPolicy`] value.
    pub fn to_runtime(&self) -> MeltdownPolicy {
        MeltdownPolicy::new(
            self.child_max_restarts,
            Duration::from_secs(self.child_window_secs),
            self.group_max_failures,
            Duration::from_secs(self.group_window_secs),
            self.supervisor_max_failures,
            Duration::from_secs(self.supervisor_window_secs),
            Duration::from_secs(self.reset_after_secs),
        )
    }
}

impl Default for MeltdownConfig {
    /// Returns the default meltdown fuse configuration.
    fn default() -> Self {
        Self {
            child_max_restarts: default_meltdown_child_max_restarts(),
            child_window_secs: default_meltdown_child_window_secs(),
            group_max_failures: default_meltdown_group_max_failures(),
            group_window_secs: default_meltdown_group_window_secs(),
            supervisor_max_failures: default_meltdown_supervisor_max_failures(),
            supervisor_window_secs: default_meltdown_supervisor_window_secs(),
            reset_after_secs: default_meltdown_reset_after_secs(),
        }
    }
}

/// Supervision pipeline capacities loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct SupervisionPipelineConfig {
    /// Event journal capacity used by the supervision pipeline.
    #[config(default = 100)]
    #[serde(default = "default_pipeline_journal_capacity")]
    pub journal_capacity: usize,
    /// Subscriber queue capacity used by the supervision pipeline.
    #[config(default = 10)]
    #[serde(default = "default_pipeline_subscriber_capacity")]
    pub subscriber_capacity: usize,
    /// Maximum concurrent restarts allowed for one supervisor instance.
    #[config(default = 5)]
    #[serde(default = "default_concurrent_restart_limit")]
    pub concurrent_restart_limit: u32,
}

impl Default for SupervisionPipelineConfig {
    /// Returns the default supervision pipeline capacities.
    fn default() -> Self {
        Self {
            journal_capacity: default_pipeline_journal_capacity(),
            subscriber_capacity: default_pipeline_subscriber_capacity(),
            concurrent_restart_limit: default_concurrent_restart_limit(),
        }
    }
}

/// Dynamic child acceptance policy loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct DynamicSupervisorConfig {
    /// Whether runtime child additions are accepted.
    #[config(default = true)]
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Optional maximum number of declared and dynamic children.
    #[serde(default)]
    pub child_limit: Option<usize>,
}

impl DynamicSupervisorConfig {
    /// Converts this YAML-friendly config into the runtime dynamic policy.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`DynamicSupervisorPolicy`] value.
    pub fn to_runtime(&self) -> DynamicSupervisorPolicy {
        DynamicSupervisorPolicy {
            enabled: self.enabled,
            child_limit: self.child_limit,
        }
    }
}

impl Default for DynamicSupervisorConfig {
    /// Returns the default dynamic supervisor policy.
    fn default() -> Self {
        Self {
            enabled: true,
            child_limit: None,
        }
    }
}

/// Restart limit configuration loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct RestartLimitConfig {
    /// Maximum restart count inside the configured window.
    pub max_restarts: u32,
    /// Window used to count restarts, in milliseconds.
    pub window_ms: u64,
}

impl RestartLimitConfig {
    /// Converts this YAML-friendly config into a runtime restart limit.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`RestartLimit`] value.
    pub fn to_runtime(&self) -> RestartLimit {
        RestartLimit::new(self.max_restarts, Duration::from_millis(self.window_ms))
    }
}

/// Group-level configuration loaded from YAML.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Config, JsonSchema)]
pub struct GroupConfig {
    /// Low-cardinality group name shared by member children.
    pub name: String,
    /// Child names that belong to this group.
    #[config(default = [])]
    #[serde(default)]
    pub children: Vec<String>,
    /// Optional group-specific restart budget override.
    #[serde(default)]
    pub budget: Option<RestartBudgetConfig>,
}

impl GroupConfig {
    /// Converts this YAML-friendly config into a runtime group config.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`RuntimeGroupConfig`] value.
    pub fn to_runtime(&self) -> RuntimeGroupConfig {
        RuntimeGroupConfig::new(
            self.name.clone(),
            self.children.iter().map(ChildId::new).collect(),
            self.budget.as_ref().map(RestartBudgetConfig::to_runtime),
        )
    }
}

/// Group strategy override loaded from YAML.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Config, JsonSchema)]
pub struct GroupStrategyConfig {
    /// Group name that owns the strategy override.
    pub group: String,
    /// Restart strategy used when a member child fails.
    pub strategy: SupervisionStrategy,
    /// Optional group-level restart limit.
    #[serde(default)]
    pub restart_limit: Option<RestartLimitConfig>,
    /// Optional escalation policy for this group.
    #[serde(default)]
    pub escalation_policy: Option<EscalationPolicy>,
}

impl GroupStrategyConfig {
    /// Converts this YAML-friendly config into a runtime group strategy.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`GroupStrategy`] value.
    pub fn to_runtime(&self) -> GroupStrategy {
        let mut strategy = GroupStrategy::new(self.group.clone(), self.strategy);
        strategy.restart_limit = self
            .restart_limit
            .as_ref()
            .map(RestartLimitConfig::to_runtime);
        strategy.escalation_policy = self.escalation_policy;
        strategy
    }
}

/// Child strategy override loaded from YAML.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Config, JsonSchema)]
pub struct ChildStrategyOverrideConfig {
    /// Child name that owns the strategy override.
    pub child_id: String,
    /// Restart strategy used for this child.
    pub strategy: SupervisionStrategy,
    /// Optional child-level restart limit.
    #[serde(default)]
    pub restart_limit: Option<RestartLimitConfig>,
    /// Optional escalation policy for this child.
    #[serde(default)]
    pub escalation_policy: Option<EscalationPolicy>,
}

impl ChildStrategyOverrideConfig {
    /// Converts this YAML-friendly config into a runtime child strategy override.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`ChildStrategyOverride`] value.
    pub fn to_runtime(&self) -> ChildStrategyOverride {
        let mut override_config =
            ChildStrategyOverride::new(ChildId::new(&self.child_id), self.strategy);
        override_config.restart_limit = self
            .restart_limit
            .as_ref()
            .map(RestartLimitConfig::to_runtime);
        override_config.escalation_policy = self.escalation_policy;
        override_config
    }
}

/// Group dependency edge loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct GroupDependencyConfig {
    /// Group that depends on another group.
    pub from_group: String,
    /// Group that is depended on.
    pub to_group: String,
    /// Failure propagation policy.
    pub propagation: PropagationPolicy,
}

impl GroupDependencyConfig {
    /// Converts this YAML-friendly config into a runtime dependency edge.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`GroupDependencyEdge`] value.
    pub fn to_runtime(&self) -> GroupDependencyEdge {
        GroupDependencyEdge {
            from_group: self.from_group.clone(),
            to_group: self.to_group.clone(),
            propagation: self.propagation,
        }
    }
}

/// Severity default loaded from YAML.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Config, JsonSchema)]
pub struct SeverityDefaultConfig {
    /// Task role that receives this default severity.
    pub task_role: TaskRole,
    /// Severity assigned to the task role.
    pub severity: SeverityClass,
}

/// Returns the default restart budget window in seconds.
fn default_restart_budget_window_secs() -> u64 {
    60
}

/// Returns the default restart budget burst.
fn default_restart_budget_max_burst() -> u32 {
    10
}

/// Returns the default restart budget recovery rate per second.
fn default_restart_budget_recovery_rate() -> f64 {
    0.5
}

/// Returns the default failure window width in seconds.
fn default_failure_window_secs() -> u64 {
    60
}

/// Returns the default retained failure count.
fn default_failure_window_max_count() -> usize {
    5
}

/// Returns the default failure threshold.
fn default_failure_window_threshold() -> usize {
    5
}

/// Returns the default child meltdown limit.
fn default_meltdown_child_max_restarts() -> u32 {
    3
}

/// Returns the default child meltdown window in seconds.
fn default_meltdown_child_window_secs() -> u64 {
    10
}

/// Returns the default group meltdown limit.
fn default_meltdown_group_max_failures() -> u32 {
    5
}

/// Returns the default group meltdown window in seconds.
fn default_meltdown_group_window_secs() -> u64 {
    30
}

/// Returns the default supervisor meltdown limit.
fn default_meltdown_supervisor_max_failures() -> u32 {
    10
}

/// Returns the default supervisor meltdown window in seconds.
fn default_meltdown_supervisor_window_secs() -> u64 {
    60
}

/// Returns the default stable reset window in seconds.
fn default_meltdown_reset_after_secs() -> u64 {
    120
}

/// Returns the default supervision pipeline journal capacity.
fn default_pipeline_journal_capacity() -> usize {
    100
}

/// Returns the default supervision pipeline subscriber capacity.
fn default_pipeline_subscriber_capacity() -> usize {
    10
}

/// Returns the default concurrent restart limit.
fn default_concurrent_restart_limit() -> u32 {
    5
}

/// Serde default helper: returns true.
fn default_true() -> bool {
    true
}