Skip to content

Commit

Permalink
add client shutdown timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Oct 2, 2018
1 parent 91af3ca commit 16945a5
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [0.7.9] - 2018-09-x

### Added

* Added client shutdown timeout setting

* Added slow request timeout setting


### Fixed

* HTTP1 decoding errors are reported to the client. #512
Expand Down
8 changes: 4 additions & 4 deletions src/server/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ where
/// Applies timeout to request prcoessing.
pub(crate) struct AcceptorTimeout<T> {
inner: T,
timeout: usize,
timeout: u64,
}

impl<T: NewService> AcceptorTimeout<T> {
pub(crate) fn new(timeout: usize, inner: T) -> Self {
pub(crate) fn new(timeout: u64, inner: T) -> Self {
Self { inner, timeout }
}
}
Expand All @@ -204,7 +204,7 @@ impl<T: NewService> NewService for AcceptorTimeout<T> {
#[doc(hidden)]
pub(crate) struct AcceptorTimeoutFut<T: NewService> {
fut: T::Future,
timeout: usize,
timeout: u64,
}

impl<T: NewService> Future for AcceptorTimeoutFut<T> {
Expand All @@ -215,7 +215,7 @@ impl<T: NewService> Future for AcceptorTimeoutFut<T> {
let inner = try_ready!(self.fut.poll());
Ok(Async::Ready(AcceptorTimeoutService {
inner,
timeout: self.timeout as u64,
timeout: self.timeout,
}))
}
}
Expand Down
26 changes: 8 additions & 18 deletions src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use super::KeepAlive;
pub(crate) trait ServiceProvider {
fn register(
&self, server: Server, lst: net::TcpListener, host: String,
addr: net::SocketAddr, keep_alive: KeepAlive, client_timeout: usize,
addr: net::SocketAddr, keep_alive: KeepAlive, client_timeout: u64,
client_shutdown: u64,
) -> Server;
}

/// Utility type that builds complete http pipeline
pub struct HttpServiceBuilder<F, H, A>
pub(crate) struct HttpServiceBuilder<F, H, A>
where
F: Fn() -> H + Send + Clone,
{
Expand Down Expand Up @@ -51,22 +52,9 @@ where
self
}

/// Use different acceptor factory
pub fn acceptor<A1>(self, acceptor: A1) -> HttpServiceBuilder<F, H, A1>
where
A1: AcceptorServiceFactory,
<A1::NewService as NewService>::InitError: fmt::Debug,
{
HttpServiceBuilder {
acceptor,
factory: self.factory.clone(),
no_client_timer: self.no_client_timer,
}
}

fn finish(
&self, host: String, addr: net::SocketAddr, keep_alive: KeepAlive,
client_timeout: usize,
client_timeout: u64, client_shutdown: u64,
) -> impl ServiceFactory {
let timeout = if self.no_client_timer {
0
Expand All @@ -81,6 +69,7 @@ where
app,
keep_alive,
timeout as u64,
client_shutdown,
ServerSettings::new(addr, &host, false),
);

Expand Down Expand Up @@ -137,12 +126,13 @@ where
{
fn register(
&self, server: Server, lst: net::TcpListener, host: String,
addr: net::SocketAddr, keep_alive: KeepAlive, client_timeout: usize,
addr: net::SocketAddr, keep_alive: KeepAlive, client_timeout: u64,
client_shutdown: u64,
) -> Server {
server.listen2(
"actix-web",
lst,
self.finish(host, addr, keep_alive, client_timeout),
self.finish(host, addr, keep_alive, client_timeout, client_shutdown),
)
}
}
12 changes: 10 additions & 2 deletions src/server/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,15 @@ where
} else {
trace!("Keep-alive timeout, close connection");
self.flags.insert(Flags::SHUTDOWN);
// TODO: start shutdown timer
return Ok(());

// start shutdown timer
if let Some(deadline) =
self.settings.client_shutdown_timer()
{
timer.reset(deadline)
} else {
return Ok(());
}
}
} else if let Some(deadline) = self.settings.keep_alive_expire()
{
Expand Down Expand Up @@ -548,6 +555,7 @@ mod tests {
App::new().into_handler(),
KeepAlive::Os,
5000,
2000,
ServerSettings::default(),
)
}
Expand Down
31 changes: 29 additions & 2 deletions src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ where
pub(super) factory: F,
pub(super) host: Option<String>,
pub(super) keep_alive: KeepAlive,
pub(super) client_timeout: usize,
pub(super) client_timeout: u64,
pub(super) client_shutdown: u64,
backlog: i32,
threads: usize,
exit: bool,
Expand Down Expand Up @@ -73,6 +74,7 @@ where
maxconn: 25_600,
maxconnrate: 256,
client_timeout: 5000,
client_shutdown: 5000,
sockets: Vec::new(),
}
}
Expand Down Expand Up @@ -140,11 +142,24 @@ where
/// To disable timeout set value to 0.
///
/// By default client timeout is set to 5000 milliseconds.
pub fn client_timeout(mut self, val: usize) -> Self {
pub fn client_timeout(mut self, val: u64) -> Self {
self.client_timeout = val;
self
}

/// Set server connection shutdown timeout in milliseconds.
///
/// Defines a timeout for shutdown connection. If a shutdown procedure does not complete
/// within this time, the request is dropped.
///
/// To disable timeout set value to 0.
///
/// By default client timeout is set to 5000 milliseconds.
pub fn client_shutdown(mut self, val: u64) -> Self {
self.client_shutdown = val;
self
}

/// Set server host name.
///
/// Host name is used by application router aa a hostname for url
Expand Down Expand Up @@ -480,13 +495,19 @@ impl<H: IntoHttpHandler, F: Fn() -> H + Send + Clone> HttpServer<H, F> {
.as_ref()
.map(|h| h.to_owned())
.unwrap_or_else(|| format!("{}", socket.addr));
let client_shutdown = if socket.scheme == "https" {
self.client_shutdown
} else {
0
};
srv = socket.handler.register(
srv,
socket.lst,
host,
socket.addr,
self.keep_alive,
self.client_timeout,
client_shutdown,
);
}
srv.start()
Expand Down Expand Up @@ -526,13 +547,19 @@ impl<H: IntoHttpHandler, F: Fn() -> H + Send + Clone> HttpServer<H, F> {
.as_ref()
.map(|h| h.to_owned())
.unwrap_or_else(|| format!("{}", socket.addr));
let client_shutdown = if socket.scheme == "https" {
self.client_shutdown
} else {
0
};
srv = socket.handler.register(
srv,
socket.lst,
host,
socket.addr,
self.keep_alive,
self.client_timeout,
client_shutdown,
);
}
srv
Expand Down
3 changes: 2 additions & 1 deletion src/server/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ where
let settings = WorkerSettings::new(
apps,
self.keep_alive,
self.client_timeout as u64,
self.client_timeout,
self.client_shutdown,
ServerSettings::new(addr, "127.0.0.1:8080", secure),
);

Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub use self::ssl::*;

pub use self::error::{AcceptorError, HttpDispatchError};
pub use self::service::HttpService;
pub use self::settings::{ServerSettings, WorkerSettings};
pub use self::settings::{ServerSettings, WorkerSettings, WorkerSettingsBuilder};

#[doc(hidden)]
pub use self::helpers::write_content_length;
Expand Down
Loading

0 comments on commit 16945a5

Please sign in to comment.