From 3173c9fa830b71999424028bed4ccd4e19680cb4 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 27 Sep 2018 19:34:07 -0700 Subject: [PATCH] diesable client timeout for tcp stream acceptor --- src/server/builder.rs | 22 +++++++++++++++++++--- src/server/http.rs | 12 +++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/server/builder.rs b/src/server/builder.rs index 5af9d0c8..28541820 100644 --- a/src/server/builder.rs +++ b/src/server/builder.rs @@ -18,6 +18,7 @@ pub(crate) trait ServiceProvider { ) -> Server; } +/// Utility type that builds complete http pipeline pub struct HttpServiceBuilder where F: Fn() -> H + Send + Clone, @@ -25,6 +26,7 @@ where factory: F, acceptor: A, pipeline: P, + no_client_timer: bool, } impl HttpServiceBuilder @@ -40,9 +42,15 @@ where factory, pipeline, acceptor, + no_client_timer: false, } } + pub(crate) fn no_client_timer(mut self) -> Self { + self.no_client_timer = true; + self + } + /// Use different acceptor factory pub fn acceptor(self, acceptor: A1) -> HttpServiceBuilder where @@ -52,6 +60,7 @@ where acceptor, pipeline: self.pipeline, factory: self.factory.clone(), + no_client_timer: self.no_client_timer, } } @@ -64,6 +73,7 @@ where pipeline, acceptor: self.acceptor, factory: self.factory.clone(), + no_client_timer: self.no_client_timer, } } @@ -71,6 +81,11 @@ where &self, host: Option, addr: net::SocketAddr, keep_alive: KeepAlive, client_timeout: usize, ) -> impl ServiceFactory { + let timeout = if self.no_client_timer { + 0 + } else { + client_timeout + }; let factory = self.factory.clone(); let pipeline = self.pipeline.clone(); let acceptor = self.acceptor.clone(); @@ -79,11 +94,11 @@ where let settings = WorkerSettings::new( app, keep_alive, - client_timeout as u64, + timeout as u64, ServerSettings::new(Some(addr), &host, false), ); - if client_timeout == 0 { + if timeout == 0 { Either::A(TcpAcceptor::new( settings.clone(), acceptor.create().and_then(pipeline.create(settings)), @@ -91,7 +106,7 @@ where } else { Either::B(TcpAcceptor::new( settings.clone(), - AcceptorTimeout::new(client_timeout, acceptor.create()) + AcceptorTimeout::new(timeout, acceptor.create()) .map_err(|_| ()) .and_then(pipeline.create(settings)), )) @@ -112,6 +127,7 @@ where factory: self.factory.clone(), acceptor: self.acceptor.clone(), pipeline: self.pipeline.clone(), + no_client_timer: self.no_client_timer, } } } diff --git a/src/server/http.rs b/src/server/http.rs index 49ae4f28..6d37473c 100644 --- a/src/server/http.rs +++ b/src/server/http.rs @@ -219,11 +219,13 @@ where lst, addr, scheme: "http", - handler: Box::new(HttpServiceBuilder::new( - self.factory.clone(), - DefaultAcceptor, - DefaultPipelineFactory::new(), - )), + handler: Box::new( + HttpServiceBuilder::new( + self.factory.clone(), + DefaultAcceptor, + DefaultPipelineFactory::new(), + ).no_client_timer(), + ), }); self