Skip to content

Commit

Permalink
fix: Align p2p and websocket connection reconnect timeouts
Browse files Browse the repository at this point in the history
Note, the app could have crashed after the coordinator not being online for while since the round value grew indefinitely.
  • Loading branch information
holzeis committed May 7, 2024
1 parent 77dd96a commit 67f7592
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions mobile/native/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use futures::SinkExt;
use futures::TryStreamExt;
use parking_lot::Mutex;
use rust_decimal::Decimal;
use std::cmp::min;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -35,7 +34,8 @@ use xxi_node::commons::OrderState;
use xxi_node::commons::OrderbookRequest;
use xxi_node::commons::Signature;

const WS_RECONNECT_TIMEOUT: Duration = Duration::from_millis(200);
// Set to the same timeout as the p2p connection reconnect
const WS_RECONNECT_TIMEOUT: Duration = Duration::from_secs(1);

pub fn subscribe(
secret_key: SecretKey,
Expand Down Expand Up @@ -65,7 +65,6 @@ pub fn subscribe(
Some(fcm_token)
};

let mut round = 1;
loop {
let url = url.clone();
let fcm_token = fcm_token.clone();
Expand Down Expand Up @@ -127,8 +126,6 @@ pub fn subscribe(
}
}

round = 1;

// abort handler on sending messages over a lost websocket connection.
handle.abort();
}
Expand All @@ -147,14 +144,12 @@ pub fn subscribe(
// coordinator returns an error on the websocket which the app is not ready to process.
//
// Note, this is the same issue for why we originally moved to 10101 Messages, we should
// think about a similar way to return protocol erros via the p2p connection.
let retry_interval = min(Duration::from_secs(1), WS_RECONNECT_TIMEOUT.mul_f32(round as f32));
// think about a similar way to return protocol errors via the p2p connection.
tracing::debug!(
?retry_interval,
?WS_RECONNECT_TIMEOUT,
"Reconnecting to orderbook WS after timeout"
);
tokio::time::sleep(retry_interval).await;
round *= 2;
tokio::time::sleep(WS_RECONNECT_TIMEOUT).await;
}
});

Expand Down

0 comments on commit 67f7592

Please sign in to comment.