1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 05:41:50 +01:00

Impl From<usize> and From<Option<usize>> for KeepAlive

This commit is contained in:
messense 2018-03-10 17:40:36 +08:00
parent 3dd8fdf450
commit 9a404a0c03
No known key found for this signature in database
GPG Key ID: BB41A8A2C716CCA9
3 changed files with 30 additions and 4 deletions

View File

@ -134,9 +134,9 @@ for full example.
Actix can wait for requests on a keep-alive connection. *Keep alive* Actix can wait for requests on a keep-alive connection. *Keep alive*
connection behavior is defined by server settings. connection behavior is defined by server settings.
* `KeepAlive::Timeout(75)` - enable 75 sec *keep alive* timer according * `75` or `Some(75)` or `KeepAlive::Timeout(75)` - enable 75 sec *keep alive* timer according
request and response settings. request and response settings.
* `KeepAlive::Disabled` - disable *keep alive*. * `None` or `KeepAlive::Disabled` - disable *keep alive*.
* `KeepAlive::Tcp(75)` - Use `SO_KEEPALIVE` socket option. * `KeepAlive::Tcp(75)` - Use `SO_KEEPALIVE` socket option.
```rust ```rust
@ -145,10 +145,20 @@ connection behavior is defined by server settings.
use actix_web::*; use actix_web::*;
fn main() { fn main() {
HttpServer::new(||
Application::new()
.resource("/", |r| r.h(httpcodes::HttpOk)))
.keep_alive(75); // <- Set keep-alive to 75 seconds
HttpServer::new(|| HttpServer::new(||
Application::new() Application::new()
.resource("/", |r| r.h(httpcodes::HttpOk))) .resource("/", |r| r.h(httpcodes::HttpOk)))
.keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option. .keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option.
HttpServer::new(||
Application::new()
.resource("/", |r| r.h(httpcodes::HttpOk)))
.keep_alive(None); // <- Disable keep-alive
} }
``` ```

View File

@ -44,6 +44,22 @@ pub enum KeepAlive {
Disabled, Disabled,
} }
impl From<usize> for KeepAlive {
fn from(keepalive: usize) -> Self {
KeepAlive::Timeout(keepalive)
}
}
impl From<Option<usize>> for KeepAlive {
fn from(keepalive: Option<usize>) -> Self {
if let Some(keepalive) = keepalive {
KeepAlive::Timeout(keepalive)
} else {
KeepAlive::Disabled
}
}
}
/// Pause accepting incoming connections /// Pause accepting incoming connections
/// ///
/// If socket contains some pending connection, they might be dropped. /// If socket contains some pending connection, they might be dropped.

View File

@ -124,8 +124,8 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
/// Set server keep-alive setting. /// Set server keep-alive setting.
/// ///
/// By default keep alive is set to a `Os`. /// By default keep alive is set to a `Os`.
pub fn keep_alive(mut self, val: KeepAlive) -> Self { pub fn keep_alive<T: Into<KeepAlive>>(mut self, val: T) -> Self {
self.keep_alive = val; self.keep_alive = val.into();
self self
} }