pub struct TcpServer { /* private fields */ }Expand description
TCP server with asupersync integration.
This server manages the lifecycle of connections and requests using asupersync’s structured concurrency primitives. Each connection runs in its own region, and each request gets its own task with a budget.
Implementations§
Source§impl TcpServer
impl TcpServer
Sourcepub fn new(config: ServerConfig) -> Self
pub fn new(config: ServerConfig) -> Self
Creates a new TCP server with the given configuration.
Sourcepub fn config(&self) -> &ServerConfig
pub fn config(&self) -> &ServerConfig
Returns the server configuration.
Sourcepub fn current_connections(&self) -> u64
pub fn current_connections(&self) -> u64
Returns the current number of active connections.
Sourcepub fn metrics(&self) -> ServerMetrics
pub fn metrics(&self) -> ServerMetrics
Returns a snapshot of the server’s connection pool metrics.
Sourcepub fn is_draining(&self) -> bool
pub fn is_draining(&self) -> bool
Returns true if the server is draining (shutting down gracefully).
Sourcepub fn start_drain(&self)
pub fn start_drain(&self)
Starts the drain process for graceful shutdown.
This sets the draining flag, which causes the server to:
- Stop accepting new connections
- Return 503 to new connection attempts
- Allow in-flight requests to complete
Sourcepub async fn wait_for_drain(
&self,
timeout: Duration,
poll_interval: Option<Duration>,
) -> bool
pub async fn wait_for_drain( &self, timeout: Duration, poll_interval: Option<Duration>, ) -> bool
Waits for all in-flight connections to drain, with a timeout.
Returns true if all connections drained successfully,
false if the timeout was reached with connections still active.
§Arguments
timeout- Maximum time to wait for connections to drainpoll_interval- How often to check connection count (default 10ms)
Sourcepub async fn drain(&self) -> u64
pub async fn drain(&self) -> u64
Initiates graceful shutdown and waits for connections to drain.
This is a convenience method that combines start_drain() and
wait_for_drain() using the configured drain timeout.
Returns the number of connections that were forcefully closed (0 if all drained within the timeout).
Sourcepub fn shutdown_controller(&self) -> &Arc<ShutdownController>
pub fn shutdown_controller(&self) -> &Arc<ShutdownController>
Returns a reference to the server’s shutdown controller.
This can be used to coordinate shutdown from external code, such as signal handlers or health check endpoints.
Sourcepub fn subscribe_shutdown(&self) -> ShutdownReceiver
pub fn subscribe_shutdown(&self) -> ShutdownReceiver
Returns a receiver for shutdown notifications.
Use this to receive shutdown signals in other parts of your application. Multiple receivers can be created and they will all be notified.
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Initiates server shutdown.
This triggers the shutdown process:
- Sets the draining flag to stop accepting new connections
- Notifies all shutdown receivers
- The server’s accept loop will exit and drain connections
This method is safe to call multiple times - subsequent calls are no-ops.
Sourcepub fn is_shutting_down(&self) -> bool
pub fn is_shutting_down(&self) -> bool
Checks if shutdown has been initiated.
Sourcepub async fn serve_with_shutdown<H, Fut>(
&self,
cx: &Cx,
shutdown: ShutdownReceiver,
handler: H,
) -> Result<GracefulOutcome<()>, ServerError>
pub async fn serve_with_shutdown<H, Fut>( &self, cx: &Cx, shutdown: ShutdownReceiver, handler: H, ) -> Result<GracefulOutcome<()>, ServerError>
Runs the server with graceful shutdown support.
The server will run until either:
- The provided shutdown receiver signals shutdown
- The server Cx is cancelled
- An unrecoverable error occurs
When shutdown is signaled, the server will:
- Stop accepting new connections
- Wait for existing connections to complete (up to drain_timeout)
- Return gracefully
§Example
use asupersync::signal::ShutdownController;
use fastapi_http::{TcpServer, ServerConfig};
let controller = ShutdownController::new();
let server = TcpServer::new(ServerConfig::new("127.0.0.1:8080"));
// Get a shutdown receiver
let shutdown = controller.subscribe();
// In another task, you can trigger shutdown:
// controller.shutdown();
server.serve_with_shutdown(&cx, shutdown, handler).await?;Sourcepub async fn serve<H, Fut>(
&self,
cx: &Cx,
handler: H,
) -> Result<(), ServerError>
pub async fn serve<H, Fut>( &self, cx: &Cx, handler: H, ) -> Result<(), ServerError>
Runs the server, accepting connections and handling requests.
This method will run until the server Cx is cancelled or an unrecoverable error occurs.
§Arguments
cx- The capability context for the server regionhandler- The request handler function
§Errors
Returns an error if binding fails or an unrecoverable IO error occurs.
Sourcepub async fn serve_on<H, Fut>(
&self,
cx: &Cx,
listener: TcpListener,
handler: H,
) -> Result<(), ServerError>
pub async fn serve_on<H, Fut>( &self, cx: &Cx, listener: TcpListener, handler: H, ) -> Result<(), ServerError>
Runs the server on a specific listener.
This is useful when you already have a bound listener.
Sourcepub async fn serve_handler(
&self,
cx: &Cx,
handler: Arc<dyn Handler>,
) -> Result<(), ServerError>
pub async fn serve_handler( &self, cx: &Cx, handler: Arc<dyn Handler>, ) -> Result<(), ServerError>
Runs the server with a Handler trait object.
This is the recommended way to serve an application that implements
the Handler trait (like App).
§Example
use fastapi_http::TcpServer;
use fastapi_core::{App, Handler};
use std::sync::Arc;
let app = App::builder()
.get("/", handler_fn)
.build();
let server = TcpServer::new(ServerConfig::new("127.0.0.1:8080"));
let cx = Cx::for_testing();
server.serve_handler(&cx, Arc::new(app)).await?;Sourcepub async fn serve_on_handler(
&self,
cx: &Cx,
listener: TcpListener,
handler: Arc<dyn Handler>,
) -> Result<(), ServerError>
pub async fn serve_on_handler( &self, cx: &Cx, listener: TcpListener, handler: Arc<dyn Handler>, ) -> Result<(), ServerError>
Runs the server on a specific listener with a Handler trait object.
Sourcepub async fn serve_concurrent<H, Fut>(
&self,
cx: &Cx,
scope: &Scope<'_>,
state: &mut RuntimeState,
handler: H,
) -> Result<(), ServerError>
pub async fn serve_concurrent<H, Fut>( &self, cx: &Cx, scope: &Scope<'_>, state: &mut RuntimeState, handler: H, ) -> Result<(), ServerError>
Serves HTTP requests with concurrent connection handling using asupersync Scope.
This method uses Scope::spawn_registered for proper structured concurrency,
ensuring all spawned connection tasks are tracked and can be properly drained
during shutdown.
§Arguments
cx- The asupersync context for cancellation and tracingscope- A scope for spawning connection tasksstate- Runtime state for task registrationhandler- The request handler