Skip to content

Commit

Permalink
Merge #1014
Browse files Browse the repository at this point in the history
1014: Reconnect to BitMex if the websocket disconnects r=bonomat a=Restioson

Adds a timeout to the websocket to bitmex in order to provoke a reconnect if the websocket becomes unresponsive. Fixes #919.

Co-authored-by: Restioson <[email protected]>
  • Loading branch information
bors[bot] and Restioson authored Jul 27, 2023
2 parents ffb57c2 + fc98e5f commit 4e416be
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/bitmex-stream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Serialize;
use serde_json::to_string;
use std::ops::Add;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use tokio_tungstenite::tungstenite;
Expand Down Expand Up @@ -80,18 +81,32 @@ fn subscribe_impl<const N: usize>(
))?)
.await;

let mut last_bitmex_message = Instant::now();

loop {
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(5)) => {
if last_bitmex_message.elapsed() > Duration::from_secs(20) {
yield Err(anyhow::anyhow!("BitMex websocket timed out"));
return;
}

let span = tracing::trace_span!("Ping BitMex");
span.in_scope(|| tracing::trace!("No message from BitMex in the last 5 seconds, pinging"));
let _ = connection

let res = connection
.send(tungstenite::Message::Ping([0u8; 32].to_vec()))
.instrument(span)
.await;

if let Err(e) = res {
yield Err(anyhow::anyhow!(e));
return;
}
},
msg = connection.next() => {
last_bitmex_message = Instant::now();

let msg = match msg {
Some(Ok(msg)) => {
msg
Expand Down

0 comments on commit 4e416be

Please sign in to comment.