Skip to main content

Module server

Module server 

Source
Expand description

Server lifecycle and connection management.

This module provides the building blocks for HTTP server lifecycle:

  • ShutdownSignal — Phase-aware shutdown coordination with drain timeouts
  • ShutdownPhase — Shutdown state machine (Running → Draining → ForceClosing → Stopped)
  • ConnectionManager — Active connection tracking with capacity limits
  • ConnectionGuard — RAII guard for automatic connection deregistration

These types build on the lower-level ShutdownController to provide server-specific lifecycle management with structured concurrency semantics.

§Architecture

Server Region (lifetime: until shutdown)
│
├── Acceptor (stops on ShutdownSignal::begin_drain)
│
└── Connections (tracked by ConnectionManager)
    ├── Connection[1] (holds ConnectionGuard)
    │   ├── Request[1.1]
    │   └── Request[1.2]
    └── Connection[2]

§Example

use asupersync::server::{ConnectionManager, ShutdownSignal};
use std::time::Duration;
use std::net::SocketAddr;

let signal = ShutdownSignal::new();
let manager = ConnectionManager::new(Some(10_000), signal.clone());

// Accept loop checks shutdown signal:
while !signal.is_shutting_down() {
    // let (stream, addr) = listener.accept().await?;
    if let Some(guard) = manager.register(addr) {
        // spawn connection handler with guard
    }
}

// Initiate graceful shutdown with 30s drain:
manager.begin_drain(Duration::from_secs(30));
manager.wait_all_closed().await;
signal.mark_stopped();

Re-exports§

pub use connection::ConnectionGuard;
pub use connection::ConnectionId;
pub use connection::ConnectionInfo;
pub use connection::ConnectionManager;
pub use shutdown::DrainStep;
pub use shutdown::GracefulDrainReport;
pub use shutdown::GracefulDrainSupervisor;
pub use shutdown::GracefulDrainTracker;
pub use shutdown::ShutdownPhase;
pub use shutdown::ShutdownSignal;
pub use shutdown::ShutdownStats;

Modules§

connection
Connection tracking and lifecycle management.
shutdown
Shutdown coordination for HTTP server lifecycle.