Skip to content

Commit 0eb9991

Browse files
alceLucioFranco
authored andcommitted
feat: expose tcp_nodelay for clients and servers (#145)
* expose tcp_nodelay * do not depend on difference versions of rand
1 parent 6b43f63 commit 0eb9991

6 files changed

Lines changed: 76 additions & 24 deletions

File tree

tonic-examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tower = { git = "https://github.com/tower-rs/tower" }
8080
# Required for routeguide
8181
serde = { version = "1.0", features = ["derive"] }
8282
serde_json = "1.0"
83-
rand = "0.7.2"
83+
rand = "0.6"
8484

8585
# Required for wellknown types
8686
prost-types = "0.5"

tonic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ rustls-native-certs = { version = "0.1", optional = true }
7676
[dev-dependencies]
7777
tokio = { version = "0.2", features = ["rt-core", "macros"] }
7878
static_assertions = "1.0"
79-
rand = "0.7.2"
79+
rand = "0.6"
8080
criterion = "0.3"
8181

8282
[package.metadata.docs.rs]

tonic/src/transport/endpoint.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Endpoint {
3030
pub(super) init_stream_window_size: Option<u32>,
3131
pub(super) init_connection_window_size: Option<u32>,
3232
pub(super) tcp_keepalive: Option<Duration>,
33+
pub(super) tcp_nodelay: bool,
3334
}
3435

3536
impl Endpoint {
@@ -171,6 +172,14 @@ impl Endpoint {
171172
}
172173
}
173174

175+
/// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
176+
pub fn tcp_nodelay(self, enabled: bool) -> Self {
177+
Endpoint {
178+
tcp_nodelay: enabled,
179+
..self
180+
}
181+
}
182+
174183
/// Create a channel from this config.
175184
pub async fn connect(&self) -> Result<Channel, super::Error> {
176185
Channel::connect(self.clone()).await
@@ -191,6 +200,7 @@ impl From<Uri> for Endpoint {
191200
init_stream_window_size: None,
192201
init_connection_window_size: None,
193202
tcp_keepalive: None,
203+
tcp_nodelay: true,
194204
}
195205
}
196206
}

tonic/src/transport/server.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
7778
impl 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

420433
impl 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

430450
impl Stream for TcpIncoming {

tonic/src/transport/service/connection.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ pub(crate) struct Connection {
2828
impl Connection {
2929
pub(crate) async fn new(endpoint: Endpoint) -> Result<Self, crate::Error> {
3030
#[cfg(feature = "tls")]
31-
let connector = connector(endpoint.tls.clone(), endpoint.tcp_keepalive);
31+
let connector = connector(endpoint.tls.clone())
32+
.set_keepalive(endpoint.tcp_keepalive)
33+
.set_nodelay(endpoint.tcp_nodelay);
3234

3335
#[cfg(not(feature = "tls"))]
34-
let connector = connector(endpoint.tcp_keepalive);
36+
let connector = connector()
37+
.set_keepalive(endpoint.tcp_keepalive)
38+
.set_nodelay(endpoint.tcp_nodelay);
3539

3640
let settings = Builder::new()
3741
.http2_initial_stream_window_size(endpoint.init_stream_window_size)

tonic/src/transport/service/connector.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ use tower_make::MakeConnection;
1111
use tower_service::Service;
1212

1313
#[cfg(not(feature = "tls"))]
14-
pub(crate) fn connector(tcp_keepalive: Option<Duration>) -> HttpConnector {
15-
let mut http = HttpConnector::new();
16-
http.enforce_http(false);
17-
http.set_nodelay(true);
18-
http.set_keepalive(tcp_keepalive);
19-
http
14+
pub(crate) fn connector() -> Connector {
15+
Connector::new()
2016
}
2117

2218
#[cfg(feature = "tls")]
23-
pub(crate) fn connector(tls: Option<TlsConnector>, tcp_keepalive: Option<Duration>) -> Connector {
24-
Connector::new(tls, tcp_keepalive)
19+
pub(crate) fn connector(tls: Option<TlsConnector>) -> Connector {
20+
Connector::new(tls)
2521
}
2622

2723
pub(crate) struct Connector {
@@ -31,13 +27,35 @@ pub(crate) struct Connector {
3127
}
3228

3329
impl Connector {
30+
#[cfg(not(feature = "tls"))]
31+
pub(crate) fn new() -> Self {
32+
Self {
33+
http: Self::http_connector(),
34+
}
35+
}
36+
3437
#[cfg(feature = "tls")]
35-
pub(crate) fn new(tls: Option<TlsConnector>, tcp_keepalive: Option<Duration>) -> Self {
38+
fn new(tls: Option<TlsConnector>) -> Self {
39+
Self {
40+
http: Self::http_connector(),
41+
tls,
42+
}
43+
}
44+
45+
pub(crate) fn set_nodelay(mut self, enabled: bool) -> Self {
46+
self.http.set_nodelay(enabled);
47+
self
48+
}
49+
50+
pub(crate) fn set_keepalive(mut self, duration: Option<Duration>) -> Self {
51+
self.http.set_keepalive(duration);
52+
self
53+
}
54+
55+
fn http_connector() -> HttpConnector {
3656
let mut http = HttpConnector::new();
3757
http.enforce_http(false);
38-
http.set_nodelay(true);
39-
http.set_keepalive(tcp_keepalive);
40-
Self { http, tls }
58+
http
4159
}
4260
}
4361

0 commit comments

Comments
 (0)