Skip to content

Commit

Permalink
feat: retry connection attempts for new connections too
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Jul 28, 2021
1 parent ffb38c4 commit fef624d
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl Endpoint {
Ok(())
}

/// Connects to another peer.
/// Connects to another peer, retries X times if the connection fails for any reason
///
/// 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 @@ -443,17 +443,30 @@ impl Endpoint {
&self,
peer_addr: &SocketAddr,
) -> Result<quinn::NewConnection> {
let new_connection = match self
let mut attempts = 0;

let mut new_connection = self
.quic_endpoint
.connect_with(self.client_cfg.clone(), peer_addr, CERT_SERVER_NAME)?
.await
{
Ok(new_connection) => new_connection,
Err(error) => {
debug!("create new connection check error: {:?}", error);
return Err(Error::QuinnConnectionClosed);
}
};
.await;

while new_connection.is_err() && attempts < MAX_ATTEMPTS {
attempts += 1;

new_connection = match self
.quic_endpoint
.connect_with(self.client_cfg.clone(), peer_addr, CERT_SERVER_NAME)?
.await
{
Ok(new_connection) => Ok(new_connection),
Err(error) => {
debug!("create new connection check error: {:?}", error);
Err(error)
}
};
}

let new_connection = new_connection?;

trace!("Successfully created new connection to peer: {}", peer_addr);
Ok(new_connection)
Expand Down

0 comments on commit fef624d

Please sign in to comment.