Skip to content

Commit

Permalink
fix(http2): don't send keep-alive ping when idle (#3381)
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <jinhao.hu@pingcap.com>
  • Loading branch information
HuSharp committed Nov 2, 2023
1 parent 0a4725e commit 429ad8a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/proto/h2/ping.rs
Expand Up @@ -264,7 +264,7 @@ impl Ponger {

if let Some(ref mut ka) = self.keep_alive {
ka.maybe_schedule(is_idle, &locked);
ka.maybe_ping(cx, &mut locked);
ka.maybe_ping(cx, is_idle, &mut locked);
}

if !locked.is_ping_sent() {
Expand All @@ -284,7 +284,7 @@ impl Ponger {
if let Some(ref mut ka) = self.keep_alive {
locked.update_last_read_at();
ka.maybe_schedule(is_idle, &locked);
ka.maybe_ping(cx, &mut locked);
ka.maybe_ping(cx, is_idle, &mut locked);
}

if let Some(ref mut bdp) = self.bdp {
Expand Down Expand Up @@ -448,7 +448,7 @@ impl KeepAlive {
self.timer.reset(&mut self.sleep, interval);
}

fn maybe_ping(&mut self, cx: &mut task::Context<'_>, shared: &mut Shared) {
fn maybe_ping(&mut self, cx: &mut task::Context<'_>, is_idle: bool, shared: &mut Shared) {
match self.state {
KeepAliveState::Scheduled(at) => {
if Pin::new(&mut self.sleep).poll(cx).is_pending() {
Expand All @@ -460,6 +460,10 @@ impl KeepAlive {
cx.waker().wake_by_ref(); // schedule us again
return;
}
if !self.while_idle && is_idle {
trace!("keep-alive no need to ping when idle and while_idle=false");
return;
}
trace!("keep-alive interval ({:?}) reached", self.interval);
shared.send_ping();
self.state = KeepAliveState::PingSent;
Expand Down

0 comments on commit 429ad8a

Please sign in to comment.