| 1 | use std::time::Duration; |
| 2 | |
| 3 | use tokio::sync::mpsc; |
| 4 | use tokio::task::JoinHandle; |
| 5 | |
| 6 | use crate::browser::{Browser, RendererState}; |
| 7 | use crate::targets::LoginState; |
| 8 | |
| 9 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 10 | pub enum BrowserEvent { |
| 11 | Started, |
| 12 | Died, |
| 13 | Restarted, |
| 14 | LoginRequired, |
| 15 | Hung, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Clone)] |
| 19 | pub 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 | |
| 29 | impl 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 | |
| 43 | pub struct Watchdog { |
| 44 | pub events: mpsc::Receiver<BrowserEvent>, |
| 45 | task: JoinHandle<()>, |
| 46 | } |
| 47 | |
| 48 | impl 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 | |
| 61 | impl Drop for Watchdog { |
| 62 | fn drop(&mut self) { |
| 63 | self.task.abort(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub(crate) fn next_backoff(current: Duration, max: Duration) -> Duration { |
| 68 | (current * 2).min(max) |
| 69 | } |
| 70 | |
| 71 | async 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) { |
| 89 | BrowserEvent::Restarted |
| 90 | } else { |
| 91 | BrowserEvent::Started |
| 92 | }; |
| 93 | if sender.send(event).await.is_err() { |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | let required = browser |
| 98 | .status() |
| 99 | .await |
| 100 | .is_ok_and(|status| status.login_state == LoginState::Required); |
| 101 | if required { |
| 102 | let first_seen = *login_since.get_or_insert_with(tokio::time::Instant::now); |
| 103 | if first_seen.elapsed() >= config.login_grace && !login_announced { |
| 104 | login_announced = true; |
| 105 | if sender.send(BrowserEvent::LoginRequired).await.is_err() { |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | } else { |
| 110 | login_since = None; |
| 111 | login_announced = false; |
| 112 | } |
| 113 | if last_probe.elapsed() >= config.probe_interval { |
| 114 | match browser.probe_renderer().await { |
| 115 | RendererState::Hung => probe_failures += 1, |
| 116 | _ => probe_failures = 0, |
| 117 | } |
| 118 | last_probe = tokio::time::Instant::now(); |
| 119 | if probe_failures >= config.probe_failures { |
| 120 | probe_failures = 0; |
| 121 | if sender.send(BrowserEvent::Hung).await.is_err() { |
| 122 | return; |
| 123 | } |
| 124 | if browser.restart_hung().await.is_ok() { |
| 125 | alive = false; |
| 126 | died = true; |
| 127 | login_announced = false; |
| 128 | } |
| 129 | continue; |
| 130 | } |
| 131 | } |
| 132 | tokio::time::sleep(config.poll_interval).await; |
| 133 | continue; |
| 134 | } |
| 135 | if alive { |
| 136 | alive = false; |
| 137 | login_announced = false; |
| 138 | login_since = None; |
| 139 | if browser.is_wanted() { |
| 140 | died = true; |
| 141 | if sender.send(BrowserEvent::Died).await.is_err() { |
| 142 | return; |
| 143 | } |
| 144 | tokio::time::sleep(config.died_wait).await; |
| 145 | continue; |
| 146 | } |
| 147 | } |
| 148 | if !browser.is_wanted() { |
| 149 | tokio::time::sleep(config.poll_interval).await; |
| 150 | continue; |
| 151 | } |
| 152 | if browser.ensure_running().await.is_err() { |
| 153 | tokio::time::sleep(backoff).await; |
| 154 | backoff = next_backoff(backoff, config.max_backoff); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[cfg(test)] |
| 160 | mod tests { |
| 161 | use super::*; |
| 162 | |
| 163 | #[test] |
| 164 | fn backoff_doubles_up_to_the_cap() { |
| 165 | let max = Duration::from_secs(60); |
| 166 | let mut value = Duration::from_secs(2); |
| 167 | let mut seen = Vec::new(); |
| 168 | for _ in 0..7 { |
| 169 | seen.push(value.as_secs()); |
| 170 | value = next_backoff(value, max); |
| 171 | } |
| 172 | assert_eq!(seen, [2, 4, 8, 16, 32, 60, 60]); |
| 173 | } |
| 174 | } |