Skip to content

Commit

Permalink
feat: Make total backoff duration configurable
Browse files Browse the repository at this point in the history
This is how the `backoff` crate supports limiting how long we back off
for. The default is 15 mins (!) which is crazy long for our purposes.

The configuration is exposed via the `Config` struct under
`retry_duration_msec`. The default is 30s, which is completely arbitrary
but reasonable-sounding. Retry logic was refactored into a method to
make it easier to use the configuration.
  • Loading branch information
Chris Connelly authored and joshuef committed Jul 28, 2021
1 parent dd27c07 commit 75d26ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ pub struct Config {
/// Duration of a UPnP port mapping.
#[structopt(long)]
pub upnp_lease_duration: Option<u32>,
/// How long to retry establishing connections and sending messages.
///
/// The duration is in milliseconds. Setting this to 0 will effectively disable retries.
#[structopt(long, default_value = "30000")]
pub retry_duration_msec: u64,
}

/// To be used to read and write our certificate and private key to disk esp. as a part of our
Expand Down
22 changes: 16 additions & 6 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl Endpoint {
Ok(())
}

/// Connects to another peer, retries X times if the connection fails for any reason
/// Connects to another peer, retries for `config.retry_duration_msec` if the connection fails.
///
/// Returns `Connection` which is a handle for sending messages to the peer and
/// `IncomingMessages` which is a stream of messages received from the peer.
Expand Down Expand Up @@ -364,7 +364,7 @@ impl Endpoint {

/// Attempt a connection to a node_addr using exponential backoff
async fn attempt_connection(&self, node_addr: &SocketAddr) -> Result<quinn::NewConnection> {
retry(ExponentialBackoff::default(), || async {
self.retry(|| async {
trace!("Attempting to connect to {:?}", node_addr);
let connecting = match self.quic_endpoint.connect_with(
self.client_cfg.clone(),
Expand Down Expand Up @@ -510,10 +510,8 @@ impl Endpoint {
}
self.connect_to(dest).await?;

retry(ExponentialBackoff::default(), || async {
Ok(self.try_send_message(msg.clone(), dest).await?)
})
.await
self.retry(|| async { Ok(self.try_send_message(msg.clone(), dest).await?) })
.await
}

/// Close all the connections of this endpoint immediately and stop accepting new connections.
Expand Down Expand Up @@ -564,4 +562,16 @@ impl Endpoint {
pub fn bootstrap_nodes(&self) -> &[SocketAddr] {
&self.bootstrap_nodes
}

fn retry<I, E, Fn, Fut>(&self, op: Fn) -> impl futures::Future<Output = Result<I, E>>
where
Fn: FnMut() -> Fut,
Fut: futures::Future<Output = Result<I, backoff::Error<E>>>,
{
let backoff = ExponentialBackoff {
max_elapsed_time: Some(Duration::from_millis(self.qp2p_config.retry_duration_msec)),
..Default::default()
};
retry(backoff, op)
}
}

0 comments on commit 75d26ea

Please sign in to comment.