BlueGit

ict rusty-teams

Files

rusty-teams / crates / browser / src / watchdog.rs

watchdog.rs

watchdog.rs 5.1 KiB Download Raw
1use std::time::Duration;
2
3use tokio::sync::mpsc;
4use tokio::task::JoinHandle;
5
6use crate::browser::{Browser, RendererState};
7use crate::targets::LoginState;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum BrowserEvent {
11 Started,
12 Died,
13 Restarted,
14 LoginRequired,
15 Hung,
16}
17
18#[derive(Debug, Clone)]
19pub struct WatchdogConfig {
20 pub poll_interval: Duration,
21 pub initial_backoff: Duration,
22 pub max_backoff: Duration,
23 pub died_wait: Duration,
24 pub probe_interval: Duration,
25 pub probe_failures: u32,
26 pub login_grace: Duration,
27}
28
29impl Default for WatchdogConfig {
30 fn default() -> Self {
31 WatchdogConfig {
32 poll_interval: Duration::from_millis(500),
33 initial_backoff: Duration::from_millis(500),
34 max_backoff: Duration::from_secs(10),
35 died_wait: Duration::from_millis(1000),
36 probe_interval: Duration::from_secs(30),
37 probe_failures: 2,
38 login_grace: Duration::from_secs(4),
39 }
40 }
41}
42
43pub struct Watchdog {
44 pub events: mpsc::Receiver<BrowserEvent>,
45 task: JoinHandle<()>,
46}
47
48impl Watchdog {
49 pub fn spawn(browser: Browser, config: WatchdogConfig) -> Watchdog {
50 let (sender, events) = mpsc::channel(32);
51 browser.set_wanted(true);
52 let task = tokio::spawn(run(browser, config, sender));
53 Watchdog { events, task }
54 }
55
56 pub fn stop(self) {
57 self.task.abort();
58 }
59}
60
61impl Drop for Watchdog {
62 fn drop(&mut self) {
63 self.task.abort();
64 }
65}
66
67pub(crate) fn next_backoff(current: Duration, max: Duration) -> Duration {
68 (current * 2).min(max)
69}
70
71async fn run(browser: Browser, config: WatchdogConfig, sender: mpsc::Sender<BrowserEvent>) {
72 let mut backoff = config.initial_backoff;
73 let mut alive = false;
74 let mut died = false;
75 let mut login_announced = false;
76 let mut login_since: Option<tokio::time::Instant> = None;
77 let mut probe_failures = 0;
78 let mut last_probe = tokio::time::Instant::now();
79 loop {
80 if browser.is_busy() {
81 tokio::time::sleep(config.poll_interval.min(Duration::from_millis(250))).await;
82 continue;
83 }
84 if browser.is_running().await {
85 backoff = config.initial_backoff;
86 if !alive {
87 alive = true;
88 let event = if std::mem::take(&mut died) { BrowserEvent::Restarted } else { BrowserEvent::Started };
89 if sender.send(event).await.is_err() {
90 return;
91 }
92 }
93 let required = browser
94 .status()
95 .await
96 .is_ok_and(|status| status.login_state == LoginState::Required);
97 if required {
98 let first_seen = *login_since.get_or_insert_with(tokio::time::Instant::now);
99 if first_seen.elapsed() >= config.login_grace && !login_announced {
100 login_announced = true;
101 if sender.send(BrowserEvent::LoginRequired).await.is_err() {
102 return;
103 }
104 }
105 } else {
106 login_since = None;
107 login_announced = false;
108 }
109 if last_probe.elapsed() >= config.probe_interval {
110 match browser.probe_renderer().await {
111 RendererState::Hung => probe_failures += 1,
112 _ => probe_failures = 0,
113 }
114 last_probe = tokio::time::Instant::now();
115 if probe_failures >= config.probe_failures {
116 probe_failures = 0;
117 if sender.send(BrowserEvent::Hung).await.is_err() {
118 return;
119 }
120 if browser.restart_hung().await.is_ok() {
121 alive = false;
122 died = true;
123 login_announced = false;
124 }
125 continue;
126 }
127 }
128 tokio::time::sleep(config.poll_interval).await;
129 continue;
130 }
131 if alive {
132 alive = false;
133 login_announced = false;
134 login_since = None;
135 if browser.is_wanted() {
136 died = true;
137 if sender.send(BrowserEvent::Died).await.is_err() {
138 return;
139 }
140 tokio::time::sleep(config.died_wait).await;
141 continue;
142 }
143 }
144 if !browser.is_wanted() {
145 tokio::time::sleep(config.poll_interval).await;
146 continue;
147 }
148 if browser.ensure_running().await.is_err() {
149 tokio::time::sleep(backoff).await;
150 backoff = next_backoff(backoff, config.max_backoff);
151 }
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn backoff_doubles_up_to_the_cap() {
161 let max = Duration::from_secs(60);
162 let mut value = Duration::from_secs(2);
163 let mut seen = Vec::new();
164 for _ in 0..7 {
165 seen.push(value.as_secs());
166 value = next_backoff(value, max);
167 }
168 assert_eq!(seen, [2, 4, 8, 16, 32, 60, 60]);
169 }
170}