BlueGit

ict rusty-teams

Files

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

watchdog.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
watchdog.rs 4.6 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}
27
28impl Default for WatchdogConfig {
29 fn default() -> Self {
30 WatchdogConfig {
31 poll_interval: Duration::from_secs(5),
32 initial_backoff: Duration::from_secs(2),
33 max_backoff: Duration::from_secs(60),
34 died_wait: Duration::from_secs(2),
35 probe_interval: Duration::from_secs(30),
36 probe_failures: 2,
37 }
38 }
39}
40
41pub struct Watchdog {
42 pub events: mpsc::Receiver<BrowserEvent>,
43 task: JoinHandle<()>,
44}
45
46impl Watchdog {
47 pub fn spawn(browser: Browser, config: WatchdogConfig) -> Watchdog {
48 let (sender, events) = mpsc::channel(32);
49 browser.set_wanted(true);
50 let task = tokio::spawn(run(browser, config, sender));
51 Watchdog { events, task }
52 }
53
54 pub fn stop(self) {
55 self.task.abort();
56 }
57}
58
59impl Drop for Watchdog {
60 fn drop(&mut self) {
61 self.task.abort();
62 }
63}
64
65pub(crate) fn next_backoff(current: Duration, max: Duration) -> Duration {
66 (current * 2).min(max)
67}
68
69async fn run(browser: Browser, config: WatchdogConfig, sender: mpsc::Sender<BrowserEvent>) {
70 let mut backoff = config.initial_backoff;
71 let mut alive = false;
72 let mut died = false;
73 let mut login_announced = false;
74 let mut probe_failures = 0;
75 let mut last_probe = tokio::time::Instant::now();
76 loop {
77 if browser.is_busy() {
78 tokio::time::sleep(config.poll_interval.min(Duration::from_millis(250))).await;
79 continue;
80 }
81 if browser.is_running().await {
82 backoff = config.initial_backoff;
83 if !alive {
84 alive = true;
85 let event = if std::mem::take(&mut died) { BrowserEvent::Restarted } else { BrowserEvent::Started };
86 if sender.send(event).await.is_err() {
87 return;
88 }
89 }
90 let required = browser
91 .status()
92 .await
93 .is_ok_and(|status| status.login_state == LoginState::Required);
94 if required && !login_announced && sender.send(BrowserEvent::LoginRequired).await.is_err() {
95 return;
96 }
97 login_announced = required;
98 if last_probe.elapsed() >= config.probe_interval {
99 match browser.probe_renderer().await {
100 RendererState::Hung => probe_failures += 1,
101 _ => probe_failures = 0,
102 }
103 last_probe = tokio::time::Instant::now();
104 if probe_failures >= config.probe_failures {
105 probe_failures = 0;
106 if sender.send(BrowserEvent::Hung).await.is_err() {
107 return;
108 }
109 if browser.restart_hung().await.is_ok() {
110 alive = false;
111 died = true;
112 login_announced = false;
113 }
114 continue;
115 }
116 }
117 tokio::time::sleep(config.poll_interval).await;
118 continue;
119 }
120 if alive {
121 alive = false;
122 login_announced = false;
123 if browser.is_wanted() {
124 died = true;
125 if sender.send(BrowserEvent::Died).await.is_err() {
126 return;
127 }
128 tokio::time::sleep(config.died_wait).await;
129 continue;
130 }
131 }
132 if !browser.is_wanted() {
133 tokio::time::sleep(config.poll_interval).await;
134 continue;
135 }
136 if browser.ensure_running().await.is_err() {
137 tokio::time::sleep(backoff).await;
138 backoff = next_backoff(backoff, config.max_backoff);
139 }
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 #[test]
148 fn backoff_doubles_up_to_the_cap() {
149 let max = Duration::from_secs(60);
150 let mut value = Duration::from_secs(2);
151 let mut seen = Vec::new();
152 for _ in 0..7 {
153 seen.push(value.as_secs());
154 value = next_backoff(value, max);
155 }
156 assert_eq!(seen, [2, 4, 8, 16, 32, 60, 60]);
157 }
158}