From 6e902d1d5cc4739ab32fb8bdab02628c0e39c9e2 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 May 2025 00:16:21 +0100 Subject: [PATCH] feat: add HttpServer::shutdown_signal (#3644) --- .cspell.yml | 2 ++ Cargo.lock | 5 +++-- actix-web/CHANGES.md | 2 ++ actix-web/Cargo.toml | 3 ++- actix-web/src/server.rs | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.cspell.yml b/.cspell.yml index 5278c478d..f275e0a24 100644 --- a/.cspell.yml +++ b/.cspell.yml @@ -1,6 +1,8 @@ version: "0.2" words: - actix + - addrs - httparse - msrv - rustup + - zstd diff --git a/Cargo.lock b/Cargo.lock index da52f2bce..29df16251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -251,9 +251,9 @@ dependencies = [ [[package]] name = "actix-server" -version = "2.5.1" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6398974fd4284f4768af07965701efbbb5fdc0616bff20cade1bb14b77675e24" +checksum = "a65064ea4a457eaf07f2fba30b4c695bf43b721790e9530d26cb6f9019ff7502" dependencies = [ "actix-rt", "actix-service", @@ -397,6 +397,7 @@ dependencies = [ "static_assertions", "time", "tokio", + "tokio-util", "tracing", "url", "zstd", diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 091a9d8c1..01d2694c8 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Add `HttpServer::shutdown_signal()` method. +- Mark `HttpServer` as `#[must_use]`. - Update `brotli` dependency to `8`. ## 4.10.2 diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 93e084146..afa4eadbc 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -132,7 +132,7 @@ compat-routing-macros-force-pub = ["actix-web-codegen?/compat-routing-macros-for actix-codec = "0.5" actix-macros = { version = "0.2.3", optional = true } actix-rt = { version = "2.6", default-features = false } -actix-server = "2" +actix-server = "2.6" actix-service = "2" actix-utils = "3" actix-tls = { version = "3.4", default-features = false, optional = true } @@ -188,6 +188,7 @@ static_assertions = "1" tls-openssl = { package = "openssl", version = "0.10.55" } tls-rustls = { package = "rustls", version = "0.23" } tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] } +tokio-util = "0.7" zstd = "0.13" [lints] diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index ef6d26f94..d56bbe84c 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -1,6 +1,8 @@ use std::{ any::Any, - cmp, fmt, io, + cmp, fmt, + future::Future, + io, marker::PhantomData, net, sync::{Arc, Mutex}, @@ -64,6 +66,7 @@ struct Config { /// .await /// } /// ``` +#[must_use] pub struct HttpServer where F: Fn() -> I + Send + Clone + 'static, @@ -312,6 +315,37 @@ where self } + /// Specify shutdown signal from a future. + /// + /// Using this method will prevent OS signal handlers being set up. + /// + /// Typically, a `CancellationToken` will be used, but any future _can_ be. + /// + /// # Examples + /// + /// ```no_run + /// use actix_web::{App, HttpServer}; + /// use tokio_util::sync::CancellationToken; + /// + /// # #[actix_web::main] + /// # async fn main() -> std::io::Result<()> { + /// let stop_signal = CancellationToken::new(); + /// + /// HttpServer::new(move || App::new()) + /// .shutdown_signal(stop_signal.cancelled_owned()) + /// .bind(("127.0.0.1", 8080))? + /// .run() + /// .await + /// # } + /// ``` + pub fn shutdown_signal(mut self, shutdown_signal: Fut) -> Self + where + Fut: Future + Send + 'static, + { + self.builder = self.builder.shutdown_signal(shutdown_signal); + self + } + /// Sets timeout for graceful worker shutdown of workers. /// /// After receiving a stop signal, workers have this much time to finish serving requests.