Skip to content

Commit

Permalink
feat(h2): use a more reasonable timer for keep alive timeout in h2
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Feb 2, 2025
1 parent 4d339df commit 0168e11
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions http/src/h2/proto/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) struct Dispatcher<'a, TlsSt, S, ReqB> {
addr: SocketAddr,
keep_alive: Pin<&'a mut KeepAlive>,
ka_dur: Duration,
pong_timeout: Duration,
service: &'a S,
date: &'a DateTimeHandle,
_req_body: PhantomData<ReqB>,
Expand All @@ -58,6 +59,7 @@ where
addr: SocketAddr,
keep_alive: Pin<&'a mut KeepAlive>,
ka_dur: Duration,
pong_timeout: Duration,
service: &'a S,
date: &'a DateTimeHandle,
) -> Self {
Expand All @@ -66,6 +68,7 @@ where
addr,
keep_alive,
ka_dur,
pong_timeout,
service,
date,
_req_body: PhantomData,
Expand All @@ -78,6 +81,7 @@ where
addr,
mut keep_alive,
ka_dur,
pong_timeout,
service,
date,
..
Expand All @@ -96,6 +100,7 @@ where
ping_pong,
date,
ka_dur,
pong_timeout,
};

let mut queue = Queue::new();
Expand Down Expand Up @@ -163,6 +168,7 @@ struct H2PingPong<'a> {
ping_pong: PingPong,
date: &'a DateTimeHandle,
ka_dur: Duration,
pong_timeout: Duration,
}

impl Future for H2PingPong<'_> {
Expand Down Expand Up @@ -195,10 +201,9 @@ impl Future for H2PingPong<'_> {

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

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

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

Expand Down
1 change: 1 addition & 0 deletions http/src/h2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ where
addr,
timer,
self.config.keep_alive_timeout,
self.config.keep_alive_timeout,
&self.service,
self.date.get(),
);
Expand Down
1 change: 1 addition & 0 deletions http/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ where
_addr,
timer.as_mut(),
self.config.keep_alive_timeout,
self.config.keep_alive_timeout,
&self.service,
self.date.get(),
)
Expand Down

0 comments on commit 0168e11

Please sign in to comment.