Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(engineio/socket): heartbeat delay causing ping burst #392

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions crates/engineioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,28 +330,38 @@ where
.expect("Pong rx should be locked only once");

let instant = tokio::time::Instant::now();
let mut interval_tick = tokio::time::interval(interval);
interval_tick.tick().await;
// Sleep for an interval minus the time it took to get here
tokio::time::sleep(interval.saturating_sub(Duration::from_millis(
15 + instant.elapsed().as_millis() as u64,
)))
.await;

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] heartbeat sender routine started", self.id);
tracing::debug!(sid = ?self.id, "heartbeat sender routine started");

let mut interval_tick = tokio::time::interval(interval);
interval_tick.tick().await;
// Some clients send the pong packet in first. If that happens, we should consume it.
heartbeat_rx.try_recv().ok();
loop {
// Some clients send the pong packet in first. If that happens, we should consume it.
heartbeat_rx.try_recv().ok();
#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "emitting ping");

self.internal_tx
.try_send(smallvec![Packet::Ping])
.map_err(|_| Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "waiting for pong");

tokio::time::timeout(timeout, heartbeat_rx.recv())
.await
.map_err(|_| Error::HeartbeatTimeout)?
.ok_or(Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "pong received");

interval_tick.tick().await;
}
}
Expand All @@ -364,7 +374,7 @@ where
.expect("Pong rx should be locked only once");

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] heartbeat receiver routine started", self.id);
tracing::debug!(sid = ?self.id, "heartbeat receiver routine started");

loop {
tokio::time::timeout(interval + timeout, heartbeat_rx.recv())
Expand All @@ -373,7 +383,7 @@ where
.ok_or(Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] ping received, sending pong", self.id);
tracing::trace!(sid = ?self.id, "ping received, sending pong");
self.internal_tx
.try_send(smallvec![Packet::Pong])
.map_err(|_| Error::HeartbeatTimeout)?;
Expand Down