Skip to content

Commit b16b489

Browse files
evanjjen20
authored andcommitted
tonic: Server::default(): Set TCP_NODELAY to true; improve docs (hyperium#2413)
## Motivation I recently ran into problems where we accidentally "lost" the tcp_nodelay=true setting, due to refactoring some code to make it easier to test. This caused mysterious 40 ms latency increases. I hope this makes it less likely for others to make this mistake. ## Solution The documentation of the `Server::tcp_nodelay()` function said "Enabled by default", but that was only true when using `Server::builder()`. The `default()` method set this to false. To fix this: * Change Server::default() to set tcp_nodelay: true. * Change Server::builder() to just call Server::default(). * Add a test to verify the settings for nodelay and keepalive. * Document the functions to note that the TCP settings are ignored when using `serve_with_incoming`. `tcp_keepalive` already had this note. I added the same documentation to `tcp_nodelay` and to `serve_with_incoming` so it is less likely to be missed.
1 parent 1e1bd8b commit b16b489

File tree

1 file changed

+37
-7
lines changed
  • tonic/src/transport/server

1 file changed

+37
-7
lines changed

tonic/src/transport/server/mod.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Default for Server<Identity> {
124124
init_connection_window_size: None,
125125
max_concurrent_streams: None,
126126
tcp_keepalive: None,
127-
tcp_nodelay: false,
127+
tcp_nodelay: true,
128128
http2_keepalive_interval: None,
129129
http2_keepalive_timeout: DEFAULT_HTTP2_KEEPALIVE_TIMEOUT,
130130
http2_adaptive_window: None,
@@ -150,11 +150,7 @@ pub struct Router<L = Identity> {
150150
impl Server {
151151
/// Create a new server builder that can configure a [`Server`].
152152
pub fn builder() -> Self {
153-
Server {
154-
tcp_nodelay: true,
155-
accept_http1: false,
156-
..Default::default()
157-
}
153+
Self::default()
158154
}
159155
}
160156

@@ -359,7 +355,7 @@ impl<L> Server<L> {
359355
/// specified will be the time to remain idle before sending TCP keepalive
360356
/// probes.
361357
///
362-
/// Important: This setting is only respected when not using `serve_with_incoming`.
358+
/// Important: This setting is ignored when using `serve_with_incoming`.
363359
///
364360
/// Default is no keepalive (`None`)
365361
///
@@ -372,6 +368,8 @@ impl<L> Server<L> {
372368
}
373369

374370
/// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
371+
///
372+
/// Important: This setting is ignored when using `serve_with_incoming`.
375373
#[must_use]
376374
pub fn tcp_nodelay(self, enabled: bool) -> Self {
377375
Server {
@@ -621,6 +619,8 @@ impl<L> Server<L> {
621619
}
622620

623621
/// Serve the service on the provided incoming stream.
622+
///
623+
/// The `tcp_nodelay` and `tcp_keepalive` settings are ignored when using this method.
624624
pub async fn serve_with_incoming<S, I, IO, IE, ResBody>(
625625
self,
626626
svc: S,
@@ -1170,3 +1170,33 @@ where
11701170
}
11711171
}
11721172
}
1173+
1174+
#[cfg(test)]
1175+
mod tests {
1176+
use std::time::Duration;
1177+
1178+
use crate::transport::Server;
1179+
1180+
#[test]
1181+
fn server_tcp_defaults() {
1182+
const EXAMPLE_TCP_KEEPALIVE: Duration = Duration::from_secs(10);
1183+
1184+
// Using ::builder() or ::default() should do the same thing
1185+
let server_via_builder = Server::builder();
1186+
assert!(server_via_builder.tcp_nodelay);
1187+
assert_eq!(server_via_builder.tcp_keepalive, None);
1188+
let server_via_default = Server::default();
1189+
assert!(server_via_default.tcp_nodelay);
1190+
assert_eq!(server_via_default.tcp_keepalive, None);
1191+
1192+
// overriding should be possible
1193+
let server_via_builder = Server::builder()
1194+
.tcp_nodelay(false)
1195+
.tcp_keepalive(Some(EXAMPLE_TCP_KEEPALIVE));
1196+
assert!(!server_via_builder.tcp_nodelay);
1197+
assert_eq!(
1198+
server_via_builder.tcp_keepalive,
1199+
Some(EXAMPLE_TCP_KEEPALIVE)
1200+
);
1201+
}
1202+
}

0 commit comments

Comments
 (0)