Skip to content

Commit

Permalink
fix delay in message_lane_loop_is_able_to_recover_from_connection_err…
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik committed Mar 31, 2021
1 parent bdcb14a commit 18e34c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions relays/messages/src/message_lane_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub async fn run<P: MessageLane>(
) -> Result<(), String> {
let exit_signal = exit_signal.shared();
relay_utils::relay_loop(source_client, target_client)
.reconnect_delay(params.reconnect_delay)
.with_metrics(format!(
"{}_to_{}_MessageLane_{}",
P::SOURCE_NAME,
Expand Down
16 changes: 13 additions & 3 deletions relays/utils/src/relay_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait Client: Clone + Send + Sync {
/// Returns generic loop that may be customized and started.
pub fn relay_loop<SC, TC>(source_client: SC, target_client: TC) -> Loop<SC, TC, ()> {
Loop {
reconnect_delay: RECONNECT_DELAY,
source_client,
target_client,
loop_metric: None,
Expand All @@ -45,6 +46,7 @@ pub fn relay_loop<SC, TC>(source_client: SC, target_client: TC) -> Loop<SC, TC,

/// Generic relay loop.
pub struct Loop<SC, TC, LM> {
reconnect_delay: Duration,
source_client: SC,
target_client: TC,
loop_metric: Option<LM>,
Expand All @@ -58,6 +60,12 @@ pub struct LoopMetrics<SC, TC, LM> {
}

impl<SC, TC, LM> Loop<SC, TC, LM> {
/// Customize delay between reconnect attempts.
pub fn reconnect_delay(mut self, reconnect_delay: Duration) -> Self {
self.reconnect_delay = reconnect_delay;
self
}

/// Start building loop metrics using given prefix.
///
/// Panics if `prefix` is empty.
Expand All @@ -66,6 +74,7 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {

LoopMetrics {
relay_loop: Loop {
reconnect_delay: self.reconnect_delay,
source_client: self.source_client,
target_client: self.target_client,
loop_metric: None,
Expand Down Expand Up @@ -100,15 +109,15 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {
match result {
Ok(()) => break,
Err(failed_client) => loop {
async_std::task::sleep(RECONNECT_DELAY).await;
async_std::task::sleep(self.reconnect_delay).await;
if failed_client == FailedClient::Both || failed_client == FailedClient::Source {
match self.source_client.reconnect().await {
Ok(()) => (),
Err(error) => {
log::warn!(
target: "bridge",
"Failed to reconnect to source client. Going to retry in {}s: {:?}",
RECONNECT_DELAY.as_secs(),
self.reconnect_delay.as_secs(),
error,
);
continue;
Expand All @@ -122,7 +131,7 @@ impl<SC, TC, LM> Loop<SC, TC, LM> {
log::warn!(
target: "bridge",
"Failed to reconnect to target client. Going to retry in {}s: {:?}",
RECONNECT_DELAY.as_secs(),
self.reconnect_delay.as_secs(),
error,
);
continue;
Expand Down Expand Up @@ -189,6 +198,7 @@ impl<SC, TC, LM> LoopMetrics<SC, TC, LM> {
}

Ok(Loop {
reconnect_delay: self.relay_loop.reconnect_delay,
source_client: self.relay_loop.source_client,
target_client: self.relay_loop.target_client,
loop_metric: self.loop_metric,
Expand Down

0 comments on commit 18e34c0

Please sign in to comment.