pub struct ShutdownToken { /* private fields */ }Expand description
A cloneable, awaitable process-shutdown handle with one shared drain deadline.
Child tokens observe parent shutdown, while calling ShutdownToken::trigger
on any child still initiates process-wide shutdown. The deadline begins with
the first trigger and is shared by all clones and children.
Implementations§
Source§impl ShutdownToken
impl ShutdownToken
Sourcepub fn new(drain_timeout: Duration) -> Self
pub fn new(drain_timeout: Duration) -> Self
Creates a shutdown handle with the maximum duration allowed for draining.
Sourcepub fn child_token(&self) -> Self
pub fn child_token(&self) -> Self
Returns a child which is cancelled by this token.
Triggering the child deliberately triggers the shared process token too.
Sourcepub fn trigger(&self) -> bool
pub fn trigger(&self) -> bool
Initiates process-wide shutdown and returns whether this was the first trigger.
Sourcepub fn on_drain<F>(&self, hook: F)
pub fn on_drain<F>(&self, hook: F)
Registers a fast synchronous action to run when drain begins.
Hooks run exactly once on the first ShutdownToken::trigger, before
cancellation wakes asynchronous drain work. A hook registered after
drain has started runs immediately. This is the dependency-neutral
composition seam for closing baukit_ops::TrafficGate without a watcher
task:
let shutdown = ShutdownToken::new(Duration::from_secs(30));
shutdown.on_drain({
let traffic_gate = traffic_gate.clone();
move || traffic_gate.stop_accepting()
});Hooks should only flip in-memory state or perform similarly bounded work;
asynchronous cleanup belongs in ShutdownToken::run_during_drain.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns whether this token has observed shutdown.
Sourcepub const fn drain_timeout(&self) -> Duration
pub const fn drain_timeout(&self) -> Duration
Returns the configured drain timeout.
Sourcepub fn deadline(&self) -> Option<Instant>
pub fn deadline(&self) -> Option<Instant>
Returns the shared absolute drain deadline after shutdown has begun.
Sourcepub async fn run_during_drain<F>(
&self,
future: F,
) -> Result<F::Output, DrainTimeout>where
F: Future,
pub async fn run_during_drain<F>(
&self,
future: F,
) -> Result<F::Output, DrainTimeout>where
F: Future,
Waits for shutdown, then runs future within the remaining drain budget.
Every call uses the same absolute deadline, so sequential cleanup steps
cannot accidentally receive a fresh timeout. On expiry, the future is
dropped and DrainTimeout is returned.
Sourcepub async fn shutdown_on_signal(&self) -> Result<ShutdownSignal>
pub async fn shutdown_on_signal(&self) -> Result<ShutdownSignal>
Waits for SIGINT/SIGTERM (or the platform Ctrl-C event), then triggers shutdown.
Sourcepub fn spawn_signal_listener(&self) -> JoinHandle<Result<ShutdownSignal>> ⓘ
pub fn spawn_signal_listener(&self) -> JoinHandle<Result<ShutdownSignal>> ⓘ
Spawns a Tokio task which triggers shutdown on the first process signal.
The returned task reports signal-handler setup failures when joined.