Expand description
Graceful shutdown coordination for the server.
This module provides graceful shutdown using asupersync’s structured concurrency:
§Shutdown Phases
- Stop accepting: Server stops accepting new connections
- Shutdown flag: New requests receive 503 Service Unavailable
- Grace period: In-flight requests get remaining time to complete
- Cancellation: Remaining requests cancelled after grace period
- Shutdown hooks: Registered cleanup callbacks run
- Region close: Server region fully closed
§Signal Handling
- SIGTERM/SIGINT triggers graceful shutdown
- Second signal forces immediate exit
- Custom signals supported via configuration
§Example
ⓘ
use fastapi_core::shutdown::{ShutdownController, GracefulShutdown};
use std::time::Duration;
async fn run_server() {
let controller = ShutdownController::new();
// Register with signals
controller.listen_for_signals();
// Run with graceful shutdown
let shutdown = GracefulShutdown::new(controller.subscribe())
.grace_period(Duration::from_secs(30));
let result = shutdown.run(server_main()).await;
match result {
ShutdownOutcome::Completed(r) => println!("Server completed: {:?}", r),
ShutdownOutcome::GracefulShutdown => println!("Graceful shutdown"),
ShutdownOutcome::ForcedShutdown => println!("Forced shutdown"),
}
}Structs§
- Graceful
Config - Configuration for graceful shutdown.
- Graceful
Shutdown - Builder for graceful shutdown.
- InFlight
Guard - RAII guard for tracking in-flight requests.
- Shutdown
Controller - Controller for initiating and coordinating shutdown.
- Shutdown
Receiver - Receiver for shutdown signals.
Enums§
- Shutdown
Hook - A shutdown hook to run during cleanup.
- Shutdown
Outcome - Outcome of running with graceful shutdown.
- Shutdown
Phase - Current phase of the shutdown process.
Traits§
- Shutdown
Aware - Extension trait for checking shutdown status from request context.
Functions§
- grace_
expired_ cancel_ reason - Create a cancel reason for grace period expiry.
- shutdown_
cancel_ reason - Create a cancel reason for server shutdown.
- subdivide_
grace_ budget - Calculate subdivided budget for a request during grace period.