Skip to main content

iroh_http_core/http/server/
handle.rs

1//! `ServeHandle` — the join handle / shutdown switch returned by
2//! [`crate::http::server::serve_with_events`].
3//!
4//! Split out of `mod.rs` per Slice C.7 of #182.
5
6use std::sync::Arc;
7
8pub struct ServeHandle {
9    pub(super) join: tokio::task::JoinHandle<()>,
10    pub(super) shutdown_notify: Arc<tokio::sync::Notify>,
11    pub(super) drain_timeout: std::time::Duration,
12    /// Resolves to `true` once the serve task has fully exited.
13    pub(super) done_rx: tokio::sync::watch::Receiver<bool>,
14}
15
16impl ServeHandle {
17    pub fn shutdown(&self) {
18        self.shutdown_notify.notify_one();
19    }
20    pub async fn drain(self) {
21        self.shutdown();
22        let _ = self.join.await;
23    }
24    pub fn abort(&self) {
25        self.join.abort();
26    }
27    pub fn drain_timeout(&self) -> std::time::Duration {
28        self.drain_timeout
29    }
30    /// Subscribe to the serve-loop-done signal.
31    ///
32    /// The returned receiver resolves (changes to `true`) once the serve task
33    /// has fully exited, including the drain phase.
34    pub fn subscribe_done(&self) -> tokio::sync::watch::Receiver<bool> {
35        self.done_rx.clone()
36    }
37}