diff --git a/crates/iz-web/src/live.rs b/crates/iz-web/src/live.rs index 0ddd399..604bb56 100644 --- a/crates/iz-web/src/live.rs +++ b/crates/iz-web/src/live.rs @@ -40,6 +40,11 @@ pub const STREAM_RETRY: Duration = Duration::from_millis(1000); /// Silence after which the server sends a keep-alive comment, so proxies keep the stream open. pub const KEEP_ALIVE: Duration = Duration::from_secs(30); +/// How often an idle `changed` stream checks its viewer anyway, so a viewer who signed out or lost access is +/// noticed even when nothing the stream follows changes. +const RECHECK: Duration = Duration::from_secs(15); +/// The longest a `changed` stream lives. The browser then reconnects and is checked again from the start. +const MAX_STREAM_LIFE: Duration = Duration::from_secs(60 * 60); /// What a live page follows. Each variant is a thing a page is about, so a write bumps exactly the /// pages that show it (`docs/architecture.md` section 9). #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -153,10 +158,29 @@ where still_allowed, last, }; - let events = stream::unfold(follow, |mut follow| async move { + let until = tokio::time::Instant::now() + MAX_STREAM_LIFE; + let events = stream::unfold(follow, move |mut follow| async move { loop { - if !wait_any(&mut follow.wakes).await { - return None; + let woke = tokio::select! { + woke = wait_any(&mut follow.wakes) => Some(woke), + () = tokio::time::sleep(RECHECK) => None, + () = tokio::time::sleep_until(until) => Some(false), + }; + match woke { + Some(true) => {} + Some(false) => return None, + // An idle tick: only the viewer is checked, so one who lost access is let go. + None => match (follow.still_allowed)().await { + Ok(true) => continue, + Ok(false) => { + tracing::info!("live: the viewer may no longer read this; stream ended"); + return None; + } + Err(error) => { + tracing::error!(error = %error, "live: could not check the viewer"); + return None; + } + }, } match (follow.fingerprint)().await { Ok(now) if now != follow.last => {