Skip to content

Commit 5218323

Browse files
committed
chore(server): Refactor default http2 keepalive timeout config
1 parent b77e0eb commit 5218323

File tree

1 file changed

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

1 file changed

+7
-11
lines changed

tonic/src/transport/server/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ 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;
79-
8078
/// A default batteries included `transport` server.
8179
///
8280
/// This provides an easy builder pattern style builder [`Server`] on top of
@@ -98,7 +96,7 @@ pub struct Server<L = Identity> {
9896
tcp_keepalive: Option<Duration>,
9997
tcp_nodelay: bool,
10098
http2_keepalive_interval: Option<Duration>,
101-
http2_keepalive_timeout: Option<Duration>,
99+
http2_keepalive_timeout: Duration,
102100
http2_adaptive_window: Option<bool>,
103101
http2_max_pending_accept_reset_streams: Option<usize>,
104102
http2_max_header_list_size: Option<u32>,
@@ -122,7 +120,7 @@ impl Default for Server<Identity> {
122120
tcp_keepalive: None,
123121
tcp_nodelay: false,
124122
http2_keepalive_interval: None,
125-
http2_keepalive_timeout: None,
123+
http2_keepalive_timeout: Duration::from_secs(20),
126124
http2_adaptive_window: None,
127125
http2_max_pending_accept_reset_streams: None,
128126
http2_max_header_list_size: None,
@@ -285,11 +283,11 @@ impl<L> Server<L> {
285283
/// Default is 20 seconds.
286284
///
287285
#[must_use]
288-
pub fn http2_keepalive_timeout(self, http2_keepalive_timeout: Option<Duration>) -> Self {
289-
Server {
290-
http2_keepalive_timeout,
291-
..self
286+
pub fn http2_keepalive_timeout(mut self, http2_keepalive_timeout: Option<Duration>) -> Self {
287+
if let Some(timeout) = http2_keepalive_timeout {
288+
self.http2_keepalive_timeout = timeout;
292289
}
290+
self
293291
}
294292

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

656654
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));
655+
let http2_keepalive_timeout = self.http2_keepalive_timeout;
660656
let http2_adaptive_window = self.http2_adaptive_window;
661657
let http2_max_pending_accept_reset_streams = self.http2_max_pending_accept_reset_streams;
662658
let max_connection_age = self.max_connection_age;

0 commit comments

Comments
 (0)