Skip to content

Commit

Permalink
server: add logs when connection closed by ws ping/pong (#1386)
Browse files Browse the repository at this point in the history
* server: logs when connection closed ws ping/pong

* Update server/src/transport/ws.rs

* fix fmt

* fix fmt2
  • Loading branch information
niklasad1 authored May 31, 2024
1 parent b6d94c7 commit d36d3a8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions server/src/transport/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ where

let stopped = conn.stop_handle.clone().shutdown();
let rpc_service = Arc::new(rpc_service);
let mut missed_pings = 0;

tokio::pin!(stopped);

Expand All @@ -103,7 +104,7 @@ where
tokio::pin!(ws_stream);

let result = loop {
let data = match try_recv(&mut ws_stream, stopped, ping_config).await {
let data = match try_recv(&mut ws_stream, stopped, ping_config, &mut missed_pings).await {
Receive::ConnectionClosed => break Ok(Shutdown::ConnectionClosed),
Receive::Stopped => break Ok(Shutdown::Stopped),
Receive::Ok(data, stop) => {
Expand Down Expand Up @@ -264,7 +265,12 @@ enum Receive<S> {
}

/// Attempts to read data from WebSocket fails if the server was stopped.
async fn try_recv<T, S>(ws_stream: &mut T, mut stopped: S, ping_config: Option<PingConfig>) -> Receive<S>
async fn try_recv<T, S>(
ws_stream: &mut T,
mut stopped: S,
ping_config: Option<PingConfig>,
missed_pings: &mut usize,
) -> Receive<S>
where
S: Future<Output = ()> + Unpin,
T: StreamExt<Item = Result<Incoming, SokettoError>> + Unpin,
Expand All @@ -274,7 +280,6 @@ where
Some(p) => IntervalStream::new(interval_at(tokio::time::Instant::now() + p.ping_interval, p.ping_interval)),
None => IntervalStream::pending(),
};
let mut missed = 0;

tokio::pin!(inactivity_check);

Expand All @@ -298,9 +303,14 @@ where
Either::Left((Either::Right((_instant, rcv)), s)) => {
if let Some(p) = ping_config {
if last_active.elapsed() > p.inactive_limit {
missed += 1;

if missed >= p.max_failures {
*missed_pings += 1;

if *missed_pings >= p.max_failures {
tracing::debug!(
target: LOG_TARGET,
"WS ping/pong inactivity limit `{}` exceeded; closing connection",
p.max_failures,
);
break Receive::ConnectionClosed;
}
}
Expand Down

0 comments on commit d36d3a8

Please sign in to comment.