Skip to content

Commit 700587c

Browse files
committed
chore(server): Refactor default http2 keepalive timeout config
1 parent 0e6c9cb commit 700587c

File tree

1 file changed

+8
-10
lines changed
  • tonic/src/transport/server

1 file changed

+8
-10
lines changed

tonic/src/transport/server/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use tower::{
7575
type BoxService = tower::util::BoxCloneService<Request<Body>, Response<Body>, crate::BoxError>;
7676
type TraceInterceptor = Arc<dyn Fn(&http::Request<()>) -> tracing::Span + Send + Sync + 'static>;
7777

78-
const DEFAULT_HTTP2_KEEPALIVE_TIMEOUT_SECS: u64 = 20;
78+
const DEFAULT_HTTP2_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(20);
7979

8080
/// A default batteries included `transport` server.
8181
///
@@ -98,7 +98,7 @@ pub struct Server<L = Identity> {
9898
tcp_keepalive: Option<Duration>,
9999
tcp_nodelay: bool,
100100
http2_keepalive_interval: Option<Duration>,
101-
http2_keepalive_timeout: Option<Duration>,
101+
http2_keepalive_timeout: Duration,
102102
http2_adaptive_window: Option<bool>,
103103
http2_max_pending_accept_reset_streams: Option<usize>,
104104
http2_max_header_list_size: Option<u32>,
@@ -122,7 +122,7 @@ impl Default for Server<Identity> {
122122
tcp_keepalive: None,
123123
tcp_nodelay: false,
124124
http2_keepalive_interval: None,
125-
http2_keepalive_timeout: None,
125+
http2_keepalive_timeout: DEFAULT_HTTP2_KEEPALIVE_TIMEOUT,
126126
http2_adaptive_window: None,
127127
http2_max_pending_accept_reset_streams: None,
128128
http2_max_header_list_size: None,
@@ -285,11 +285,11 @@ impl<L> Server<L> {
285285
/// Default is 20 seconds.
286286
///
287287
#[must_use]
288-
pub fn http2_keepalive_timeout(self, http2_keepalive_timeout: Option<Duration>) -> Self {
289-
Server {
290-
http2_keepalive_timeout,
291-
..self
288+
pub fn http2_keepalive_timeout(mut self, http2_keepalive_timeout: Option<Duration>) -> Self {
289+
if let Some(timeout) = http2_keepalive_timeout {
290+
self.http2_keepalive_timeout = timeout;
292291
}
292+
self
293293
}
294294

295295
/// Sets whether to use an adaptive flow control. Defaults to false.
@@ -654,9 +654,7 @@ impl<L> Server<L> {
654654
let http2_only = !self.accept_http1;
655655

656656
let http2_keepalive_interval = self.http2_keepalive_interval;
657-
let http2_keepalive_timeout = self
658-
.http2_keepalive_timeout
659-
.unwrap_or_else(|| Duration::new(DEFAULT_HTTP2_KEEPALIVE_TIMEOUT_SECS, 0));
657+
let http2_keepalive_timeout = self.http2_keepalive_timeout;
660658
let http2_adaptive_window = self.http2_adaptive_window;
661659
let http2_max_pending_accept_reset_streams = self.http2_max_pending_accept_reset_streams;
662660
let max_connection_age = self.max_connection_age;

0 commit comments

Comments
 (0)