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
//! Exponential backoff with pinned integer arithmetic
use core::time::Duration;
use shep_core::config::AppConfig;
/// Compute restart delay based on app config and consecutive unstable exits.
///
/// - Fixed `restart_delay` field takes precedence if set (even over a stable exit)
/// - Else `consecutive_unstable == 0` (stable exit) → `None` (immediate restart)
/// - Else if `exp_backoff_restart_delay = Some(initial)`: uses iterative integer rule
/// `d = min(d * 3 / 2, 15000)` starting at `initial` for `consecutive_unstable = 1`,
/// applied `consecutive_unstable - 1` times. `AppConfig::default()` sets this to
/// `Some(100ms)`, so an unstable exit is throttled unless an operator opts out by
/// setting the field to `None` explicitly.
/// - Else (both unset) → `None` (immediate restart)
pub fn restart_delay(app: &AppConfig, consecutive_unstable: u32) -> Option<Duration> {
// Fixed delay takes precedence
if let Some(fixed) = app.restart_delay {
return Some(fixed.as_duration());
}
// Stable exit → immediate restart
if consecutive_unstable == 0 {
return None;
}
// Exponential backoff
if let Some(initial) = app.exp_backoff_restart_delay {
let mut d = initial.as_millis();
// Apply the iterative rule consecutive_unstable - 1 times
for _ in 1..consecutive_unstable {
d = (d * 3 / 2).min(15000);
}
return Some(Duration::from_millis(d));
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_sequence_is_pinned() {
let mut app = shep_core::config::AppConfig::minimal("p", "./p");
app.exp_backoff_restart_delay = Some("100".parse().unwrap());
let expected: [u64; 15] = [
100, 150, 225, 337, 505, 757, 1135, 1702, 2553, 3829, 5743, 8614, 12921, 15000, 15000,
];
for (i, want) in expected.iter().enumerate() {
let got = restart_delay(&app, (i + 1) as u32).unwrap();
assert_eq!(
got.as_millis() as u64,
*want,
"consecutive_unstable={}",
i + 1
);
}
}
#[test]
fn stable_exit_no_delay() {
let mut app = shep_core::config::AppConfig::minimal("p", "./p");
app.exp_backoff_restart_delay = Some("100".parse().unwrap());
assert_eq!(restart_delay(&app, 0), None);
}
#[test]
fn fixed_delay_overrides_backoff() {
let mut app = shep_core::config::AppConfig::minimal("p", "./p");
app.restart_delay = Some("500".parse().unwrap());
app.exp_backoff_restart_delay = Some("100".parse().unwrap());
assert_eq!(restart_delay(&app, 1).unwrap().as_millis(), 500);
}
/// Defect 2 fix: an app with neither `restart_delay` nor
/// `exp_backoff_restart_delay` set is not "no delay" any more:
/// `AppConfig::default()` now carries a 100ms `exp_backoff_restart_delay`
/// (see its doc comment), so an unconfigured app still backs off on
/// unstable exits.
#[test]
fn default_config_backs_off_unstable_exits() {
let app = shep_core::config::AppConfig::minimal("p", "./p");
assert_eq!(restart_delay(&app, 1).unwrap().as_millis(), 100);
assert_eq!(restart_delay(&app, 2).unwrap().as_millis(), 150);
}
/// A stable exit (`consecutive_unstable == 0`) is unaffected by the
/// default above: it stays `None` regardless of what's configured, so a
/// healthy app restarting during a deploy is never throttled.
#[test]
fn stable_exit_is_never_delayed_even_with_a_configured_backoff() {
let app = shep_core::config::AppConfig::minimal("p", "./p");
assert_eq!(restart_delay(&app, 0), None);
}
/// Explicitly turning the backoff off (`exp_backoff_restart_delay =
/// None`) is still honoured: this is the escape hatch documented on the
/// field for an operator who wants the old unthrottled behaviour back.
#[test]
fn an_explicit_none_still_means_no_backoff() {
let mut app = shep_core::config::AppConfig::minimal("p", "./p");
app.exp_backoff_restart_delay = None;
assert_eq!(restart_delay(&app, 1), None);
assert_eq!(restart_delay(&app, 5), None);
}
/// The only opt-out an operator can actually write is the string `"0"`:
/// `AppConfig`'s struct-level `#[serde(default)]` means an omitted key
/// deserializes to `Some(100ms)`, not `None` -- TOML has no way to write
/// a bare null. Parses a real Flockfile end to end, rather than poking
/// the struct field directly, so a Deserialize regression in the
/// grammar or the default would show up here too.
#[test]
fn the_toml_escape_hatch_is_the_string_zero_not_a_missing_key() {
let src = r#"
[[app]]
name = "p"
script = "./p"
exp_backoff_restart_delay = "0"
"#;
let flock =
shep_core::config::Flockfile::parse(src, shep_core::config::FlockFormat::Toml).unwrap();
let app = &flock.apps[0];
assert_eq!(
app.exp_backoff_restart_delay,
Some(shep_core::values::UpDuration::from_millis(0))
);
assert_eq!(restart_delay(app, 1), Some(Duration::from_millis(0)));
}
}