Idle live streams re-check the viewer every 15 s and end after an hour
Browse files at this commitcef009cde83f6af0600bfbcaa7b4891362d6d6abParent: 67e1cce- fix
- idle live streams re-check the viewer every 15 s and end after an hour
1 changed file +27 -3
- crates/iz-web/src/live.rs +27 -3
crates/iz-web/src/live.rsmodified+27 -3
| @@ -40,6 +40,11 @@ pub const STREAM_RETRY: Duration = Duration::from_millis(1000); | |||
| 40 | /// Silence after which the server sends a keep-alive comment, so proxies keep the stream open. | 40 | /// Silence after which the server sends a keep-alive comment, so proxies keep the stream open. |
| 41 | pub const KEEP_ALIVE: Duration = Duration::from_secs(30); | 41 | pub const KEEP_ALIVE: Duration = Duration::from_secs(30); |
| 42 | 42 | ||
| 43 | +/// How often an idle `changed` stream checks its viewer anyway, so a viewer who signed out or lost access is | ||
| 44 | +/// noticed even when nothing the stream follows changes. | ||
| 45 | +const RECHECK: Duration = Duration::from_secs(15); | ||
| 46 | +/// The longest a `changed` stream lives. The browser then reconnects and is checked again from the start. | ||
| 47 | +const MAX_STREAM_LIFE: Duration = Duration::from_secs(60 * 60); | ||
| 43 | /// What a live page follows. Each variant is a thing a page is about, so a write bumps exactly the | 48 | /// What a live page follows. Each variant is a thing a page is about, so a write bumps exactly the |
| 44 | /// pages that show it (`docs/architecture.md` section 9). | 49 | /// pages that show it (`docs/architecture.md` section 9). |
| 45 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | 50 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| @@ -153,10 +158,29 @@ where | |||
| 153 | still_allowed, | 158 | still_allowed, |
| 154 | last, | 159 | last, |
| 155 | }; | 160 | }; |
| 156 | - let events = stream::unfold(follow, |mut follow| async move { | 161 | + let until = tokio::time::Instant::now() + MAX_STREAM_LIFE; |
| 162 | + let events = stream::unfold(follow, move |mut follow| async move { | ||
| 157 | loop { | 163 | loop { |
| 158 | - if !wait_any(&mut follow.wakes).await { | 164 | + let woke = tokio::select! { |
| 159 | - return None; | 165 | + woke = wait_any(&mut follow.wakes) => Some(woke), |
| 166 | + () = tokio::time::sleep(RECHECK) => None, | ||
| 167 | + () = tokio::time::sleep_until(until) => Some(false), | ||
| 168 | + }; | ||
| 169 | + match woke { | ||
| 170 | + Some(true) => {} | ||
| 171 | + Some(false) => return None, | ||
| 172 | + // An idle tick: only the viewer is checked, so one who lost access is let go. | ||
| 173 | + None => match (follow.still_allowed)().await { | ||
| 174 | + Ok(true) => continue, | ||
| 175 | + Ok(false) => { | ||
| 176 | + tracing::info!("live: the viewer may no longer read this; stream ended"); | ||
| 177 | + return None; | ||
| 178 | + } | ||
| 179 | + Err(error) => { | ||
| 180 | + tracing::error!(error = %error, "live: could not check the viewer"); | ||
| 181 | + return None; | ||
| 182 | + } | ||
| 183 | + }, | ||
| 160 | } | 184 | } |
| 161 | match (follow.fingerprint)().await { | 185 | match (follow.fingerprint)().await { |
| 162 | Ok(now) if now != follow.last => { | 186 | Ok(now) if now != follow.last => { |