From 04f6f0bd025d4dc271dde9793f6d7cd3eeed2d4a Mon Sep 17 00:00:00 2001 From: Jonas Fassbender Date: Fri, 3 Mar 2023 16:07:26 +0100 Subject: [PATCH] Fixed keep-alive on response example (#316) * fixed keep-alive on response example * fixed dead code and unused variable errors --- docs/server.md | 1 + examples/server/src/keep_alive_tp.rs | 16 +++++++++++----- examples/server/src/main.rs | 1 + 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/server.md b/docs/server.md index 848551a..a95663b 100644 --- a/docs/server.md +++ b/docs/server.md @@ -97,6 +97,7 @@ Actix Web keeps connections open to wait for subsequent requests. If the first option above is selected, then keep-alive is enabled for HTTP/1.1 requests if the response does not explicitly disallow it by, for example, setting the [connection type][httpconnectiontype] to `Close` or `Upgrade`. Force closing a connection can be done with [the `force_close()` method on `HttpResponseBuilder`](https://docs.rs/actix-web/4/actix_web/struct.HttpResponseBuilder.html#method.force_close) > Keep-alive is **off** for HTTP/1.0 and is **on** for HTTP/1.1 and HTTP/2.0. + ## Graceful shutdown diff --git a/examples/server/src/keep_alive_tp.rs b/examples/server/src/keep_alive_tp.rs index 003b730..a34c065 100644 --- a/examples/server/src/keep_alive_tp.rs +++ b/examples/server/src/keep_alive_tp.rs @@ -1,11 +1,17 @@ +#![allow(dead_code)] + // use actix_web::{http, HttpRequest, HttpResponse}; -async fn index(req: HttpRequest) -> HttpResponse { - HttpResponse::Ok() - .connection_type(http::ConnectionType::Close) // <- Close connection - .force_close() // <- Alternative method - .finish() +async fn index(_req: HttpRequest) -> HttpResponse { + let mut resp = HttpResponse::Ok() + .force_close() // <- Close connection on HttpResponseBuilder + .finish(); + + // Alternatively close connection on the HttpResponse struct + resp.head_mut().set_connection_type(http::ConnectionType::Close); + + resp } // diff --git a/examples/server/src/main.rs b/examples/server/src/main.rs index 77155d2..505caaa 100644 --- a/examples/server/src/main.rs +++ b/examples/server/src/main.rs @@ -1,4 +1,5 @@ pub mod keep_alive; +pub mod keep_alive_tp; pub mod signals; pub mod ssl; pub mod workers;