@@ -56,6 +56,7 @@ pub struct Server {
5656 init_connection_window_size : Option < u32 > ,
5757 max_concurrent_streams : Option < u32 > ,
5858 tcp_keepalive : Option < Duration > ,
59+ tcp_nodelay : bool ,
5960}
6061
6162/// A stack based `Service` router.
@@ -77,7 +78,10 @@ pub trait ServiceName {
7778impl Server {
7879 /// Create a new server builder that can configure a [`Server`].
7980 pub fn builder ( ) -> Self {
80- Default :: default ( )
81+ Server {
82+ tcp_nodelay : true ,
83+ ..Default :: default ( )
84+ }
8185 }
8286}
8387
@@ -164,6 +168,14 @@ impl Server {
164168 }
165169 }
166170
171+ /// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
172+ pub fn tcp_nodelay ( self , enabled : bool ) -> Self {
173+ Server {
174+ tcp_nodelay : enabled,
175+ ..self
176+ }
177+ }
178+
167179 /// Intercept the execution of gRPC methods.
168180 ///
169181 /// ```
@@ -221,12 +233,13 @@ impl Server {
221233 let init_connection_window_size = self . init_connection_window_size ;
222234 let init_stream_window_size = self . init_stream_window_size ;
223235 let max_concurrent_streams = self . max_concurrent_streams ;
224- let tcp_keepalive = self . tcp_keepalive ;
225236 // let timeout = self.timeout.clone();
226237
227238 let incoming = hyper:: server:: accept:: from_stream :: < _ , _ , crate :: Error > (
228239 async_stream:: try_stream! {
229- let mut tcp = TcpIncoming :: bind( addr, tcp_keepalive) ?;
240+ let mut tcp = TcpIncoming :: bind( addr) ?
241+ . set_nodelay( self . tcp_nodelay)
242+ . set_keepalive( self . tcp_keepalive) ;
230243
231244 while let Some ( stream) = tcp. try_next( ) . await ? {
232245 #[ cfg( feature = "tls" ) ]
@@ -418,13 +431,20 @@ struct TcpIncoming {
418431}
419432
420433impl TcpIncoming {
421- fn bind ( addr : SocketAddr , tcp_keepalive : Option < Duration > ) -> Result < Self , crate :: Error > {
422- let mut inner = conn:: AddrIncoming :: bind ( & addr) . map_err ( Box :: new) ?;
423- inner. set_nodelay ( true ) ;
424- inner. set_keepalive ( tcp_keepalive) ;
425-
434+ fn bind ( addr : SocketAddr ) -> Result < Self , crate :: Error > {
435+ let inner = conn:: AddrIncoming :: bind ( & addr) . map_err ( Box :: new) ?;
426436 Ok ( Self { inner } )
427437 }
438+
439+ fn set_nodelay ( mut self , enabled : bool ) -> Self {
440+ self . inner . set_nodelay ( enabled) ;
441+ self
442+ }
443+
444+ fn set_keepalive ( mut self , tcp_keepalive : Option < Duration > ) -> Self {
445+ self . inner . set_keepalive ( tcp_keepalive) ;
446+ self
447+ }
428448}
429449
430450impl Stream for TcpIncoming {
0 commit comments