Skip to main content

Module shutdown

Module shutdown 

Source
Expand description

Graceful shutdown coordination for the server.

This module provides graceful shutdown using asupersync’s structured concurrency:

§Shutdown Phases

  1. Stop accepting: Server stops accepting new connections
  2. Shutdown flag: New requests receive 503 Service Unavailable
  3. Grace period: In-flight requests get remaining time to complete
  4. Cancellation: Remaining requests cancelled after grace period
  5. Shutdown hooks: Registered cleanup callbacks run
  6. 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§

GracefulConfig
Configuration for graceful shutdown.
GracefulShutdown
Builder for graceful shutdown.
InFlightGuard
RAII guard for tracking in-flight requests.
ShutdownController
Controller for initiating and coordinating shutdown.
ShutdownReceiver
Receiver for shutdown signals.

Enums§

ShutdownHook
A shutdown hook to run during cleanup.
ShutdownOutcome
Outcome of running with graceful shutdown.
ShutdownPhase
Current phase of the shutdown process.

Traits§

ShutdownAware
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.