Skip to main content

serve_with_shutdown

Function serve_with_shutdown 

Source
pub async fn serve_with_shutdown<U, F>(
    listener: TcpListener,
    service: ProxyService<U>,
    options: ServeOptions,
    signal: F,
) -> Result<()>
where U: Upstream, F: Future<Output = ()>,
Expand description

serve_with, until signal resolves; then a graceful shutdown. The listening socket closes, so new connections are refused; a connection still in its TLS handshake or waiting for its max_connections slot is dropped; every open connection is asked to wind down (HTTP/2 gets a GOAWAY, so its client opens no new streams; HTTP/1.1 closes after the response in progress), and requests and streams in flight finish. The future resolves once every connection has closed, or after drain_timeout, closing the ones still open. Dropping it closes every connection at once.

A connection a fallback upgraded (a WebSocket) belongs to the fallback’s own task once upgraded, and closes when that task lets it go.

§Errors

See serve.

§Examples

use structured_proxy::{ProxyServer, ServeOptions};

let service = ProxyServer::from_yaml_str("service:\n  name: demo\n")?
    .service(tonic::service::Routes::default())?;
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
let shutdown = async {
    tokio::signal::ctrl_c().await.expect("the signal handler installs");
};
structured_proxy::serve_with_shutdown(listener, service, ServeOptions::new(), shutdown).await?;