Skip to content

Commit 429ad8a

Browse files
authoredNov 2, 2023
fix(http2): don't send keep-alive ping when idle (#3381)
Signed-off-by: husharp <jinhao.hu@pingcap.com>
1 parent 0a4725e commit 429ad8a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
 

‎src/proto/h2/ping.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl Ponger {
264264

265265
if let Some(ref mut ka) = self.keep_alive {
266266
ka.maybe_schedule(is_idle, &locked);
267-
ka.maybe_ping(cx, &mut locked);
267+
ka.maybe_ping(cx, is_idle, &mut locked);
268268
}
269269

270270
if !locked.is_ping_sent() {
@@ -284,7 +284,7 @@ impl Ponger {
284284
if let Some(ref mut ka) = self.keep_alive {
285285
locked.update_last_read_at();
286286
ka.maybe_schedule(is_idle, &locked);
287-
ka.maybe_ping(cx, &mut locked);
287+
ka.maybe_ping(cx, is_idle, &mut locked);
288288
}
289289

290290
if let Some(ref mut bdp) = self.bdp {
@@ -448,7 +448,7 @@ impl KeepAlive {
448448
self.timer.reset(&mut self.sleep, interval);
449449
}
450450

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

0 commit comments

Comments
 (0)
Please sign in to comment.