Skip to content

Commit 0168e11

Browse files
committed
feat(h2): use a more reasonable timer for keep alive timeout in h2
1 parent 4d339df commit 0168e11

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

http/src/h2/proto/dispatcher.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(crate) struct Dispatcher<'a, TlsSt, S, ReqB> {
3737
addr: SocketAddr,
3838
keep_alive: Pin<&'a mut KeepAlive>,
3939
ka_dur: Duration,
40+
pong_timeout: Duration,
4041
service: &'a S,
4142
date: &'a DateTimeHandle,
4243
_req_body: PhantomData<ReqB>,
@@ -58,6 +59,7 @@ where
5859
addr: SocketAddr,
5960
keep_alive: Pin<&'a mut KeepAlive>,
6061
ka_dur: Duration,
62+
pong_timeout: Duration,
6163
service: &'a S,
6264
date: &'a DateTimeHandle,
6365
) -> Self {
@@ -66,6 +68,7 @@ where
6668
addr,
6769
keep_alive,
6870
ka_dur,
71+
pong_timeout,
6972
service,
7073
date,
7174
_req_body: PhantomData,
@@ -78,6 +81,7 @@ where
7881
addr,
7982
mut keep_alive,
8083
ka_dur,
84+
pong_timeout,
8185
service,
8286
date,
8387
..
@@ -96,6 +100,7 @@ where
96100
ping_pong,
97101
date,
98102
ka_dur,
103+
pong_timeout,
99104
};
100105

101106
let mut queue = Queue::new();
@@ -163,6 +168,7 @@ struct H2PingPong<'a> {
163168
ping_pong: PingPong,
164169
date: &'a DateTimeHandle,
165170
ka_dur: Duration,
171+
pong_timeout: Duration,
166172
}
167173

168174
impl Future for H2PingPong<'_> {
@@ -195,10 +201,9 @@ impl Future for H2PingPong<'_> {
195201

196202
this.ping_pong.send_ping(Ping::opaque())?;
197203

198-
// Update the keep alive to 10 times the normal keep alive duration.
199-
// There is no particular reason for the duration choice here. as h2 connection is
200-
// suggested to be kept alive for a relative long time.
201-
let deadline = this.date.now() + (this.ka_dur * 10);
204+
// Update the keep alive to the pong timeout, if pong is not received within this
205+
// time. The connection is considered dead.
206+
let deadline = this.date.now() + this.ka_dur + this.pong_timeout;
202207

203208
this.keep_alive.as_mut().update(deadline);
204209

http/src/h2/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ where
7171
addr,
7272
timer,
7373
self.config.keep_alive_timeout,
74+
self.config.keep_alive_timeout,
7475
&self.service,
7576
self.date.get(),
7677
);

http/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ where
140140
_addr,
141141
timer.as_mut(),
142142
self.config.keep_alive_timeout,
143+
self.config.keep_alive_timeout,
143144
&self.service,
144145
self.date.get(),
145146
)

0 commit comments

Comments
 (0)