Skip to content

Commit ecfacae

Browse files
committed
feat(ws): replace connection timeout with connection deadline for improved WebSocket management
1 parent d37dc5d commit ecfacae

File tree

1 file changed

+7
-4
lines changed
  • apps/hermes/server/src/api

1 file changed

+7
-4
lines changed

apps/hermes/server/src/api/ws.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ use {
4040
},
4141
time::Duration,
4242
},
43-
tokio::sync::{broadcast::Receiver, watch},
43+
tokio::{
44+
sync::{broadcast::Receiver, watch},
45+
time::Instant,
46+
},
4447
};
4548

4649
const PING_INTERVAL_DURATION: Duration = Duration::from_secs(30);
@@ -253,7 +256,7 @@ pub struct Subscriber<S> {
253256
sender: SplitSink<WebSocket, Message>,
254257
price_feeds_with_config: HashMap<PriceIdentifier, PriceFeedClientConfig>,
255258
ping_interval: tokio::time::Interval,
256-
connection_timeout: tokio::time::Sleep,
259+
connection_deadline: Instant,
257260
exit: watch::Receiver<bool>,
258261
responded_to_ping: bool,
259262
}
@@ -282,7 +285,7 @@ where
282285
sender,
283286
price_feeds_with_config: HashMap::new(),
284287
ping_interval: tokio::time::interval(PING_INTERVAL_DURATION),
285-
connection_timeout: tokio::time::sleep(MAX_CONNECTION_DURATION),
288+
connection_deadline: Instant::now() + MAX_CONNECTION_DURATION,
286289
exit: crate::EXIT.subscribe(),
287290
responded_to_ping: true, // We start with true so we don't close the connection immediately
288291
}
@@ -328,7 +331,7 @@ where
328331
self.sender.send(Message::Ping(vec![])).await?;
329332
Ok(())
330333
},
331-
_ = &mut self.connection_timeout => {
334+
_ = tokio::time::sleep_until(self.connection_deadline) => {
332335
tracing::info!(
333336
id = self.id,
334337
ip = ?self.ip_addr,

0 commit comments

Comments
 (0)