Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
init_connection_window_size: None,
max_concurrent_streams: None,
tcp_keepalive: None,
tcp_nodelay: false,
tcp_nodelay: true,
http2_keepalive_interval: None,
http2_keepalive_timeout: DEFAULT_HTTP2_KEEPALIVE_TIMEOUT,
http2_adaptive_window: None,
Expand All @@ -148,11 +148,7 @@
impl Server {
/// Create a new server builder that can configure a [`Server`].
pub fn builder() -> Self {
Server {
tcp_nodelay: true,
accept_http1: false,
..Default::default()
}
Self::default()
}
}

Expand Down Expand Up @@ -346,7 +342,7 @@
/// specified will be the time to remain idle before sending TCP keepalive
/// probes.
///
/// Important: This setting is only respected when not using `serve_with_incoming`.
/// Important: This setting is ignored when using `serve_with_incoming`.
///
/// Default is no keepalive (`None`)
///
Expand All @@ -359,6 +355,8 @@
}

/// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
///
/// Important: This setting is ignored when using `serve_with_incoming`.
#[must_use]
pub fn tcp_nodelay(self, enabled: bool) -> Self {
Server {
Expand Down Expand Up @@ -607,6 +605,8 @@
}

/// Serve the service on the provided incoming stream.
///
/// The `tcp_nodelay` and `tcp_keepalive` settings are ignored when using this method.
pub async fn serve_with_incoming<S, I, IO, IE, ResBody>(
self,
svc: S,
Expand Down Expand Up @@ -1154,3 +1154,33 @@
}
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use crate::transport::Server;

#[test]
fn server_tcp_defaults() {
const EXAMPLE_TCP_KEEPALIVE: Duration = Duration::from_secs(10);

// Using ::builder() or ::default() should do the same thing
let server_via_builder = Server::builder();
assert_eq!(server_via_builder.tcp_nodelay, true);

Check warning on line 1170 in tonic/src/transport/server/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

used `assert_eq!` with a literal bool
assert_eq!(server_via_builder.tcp_keepalive, None);
let server_via_default = Server::default();
assert_eq!(server_via_default.tcp_nodelay, true);

Check warning on line 1173 in tonic/src/transport/server/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

used `assert_eq!` with a literal bool
assert_eq!(server_via_default.tcp_keepalive, None);

// overriding should be possible
let server_via_builder = Server::builder()
.tcp_nodelay(false)
.tcp_keepalive(Some(EXAMPLE_TCP_KEEPALIVE));
assert_eq!(server_via_builder.tcp_nodelay, false);

Check warning on line 1180 in tonic/src/transport/server/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

used `assert_eq!` with a literal bool
assert_eq!(
server_via_builder.tcp_keepalive,
Some(EXAMPLE_TCP_KEEPALIVE)
);
}
}
Loading