fix: use exp backoff for running control channel (#104)

This commit is contained in:
Yujia Qiao 2022-01-17 12:30:37 +08:00 committed by GitHub
parent 2c25bbd1cb
commit 907109984f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -8,6 +8,7 @@ use crate::protocol::{
};
use crate::transport::{SocketOpts, TcpTransport, Transport};
use anyhow::{anyhow, bail, Context, Result};
use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
use bytes::{Bytes, BytesMut};
use std::collections::HashMap;
@ -24,7 +25,7 @@ use crate::transport::NoiseTransport;
#[cfg(feature = "tls")]
use crate::transport::TlsTransport;
use crate::constants::{UDP_BUFFER_SIZE, UDP_SENDQ_SIZE, UDP_TIMEOUT};
use crate::constants::{run_control_chan_backoff, UDP_BUFFER_SIZE, UDP_SENDQ_SIZE, UDP_TIMEOUT};
// The entrypoint of running a client
pub async fn run_client(
@ -485,6 +486,7 @@ impl ControlChannelHandle {
tokio::spawn(
async move {
let mut backoff = run_control_chan_backoff();
while let Err(err) = s
.run()
.await
@ -494,9 +496,12 @@ impl ControlChannelHandle {
break;
}
let duration = Duration::from_secs(1);
error!("{:?}\n\nRetry in {:?}...", err, duration);
time::sleep(duration).await;
if let Some(duration) = backoff.next_backoff() {
error!("{:?}\n\nRetry in {:?}...", err, duration);
time::sleep(duration).await;
} else {
error!("{:?}. Break", err);
}
}
}
.instrument(Span::current()),

View File

@ -14,3 +14,11 @@ pub fn listen_backoff() -> ExponentialBackoff {
..Default::default()
}
}
pub fn run_control_chan_backoff() -> ExponentialBackoff {
ExponentialBackoff {
max_elapsed_time: None,
max_interval: Duration::from_secs(1),
..Default::default()
}
}