Skip to content

Commit

Permalink
Add exponential sleep between connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed May 3, 2022
1 parent f6e7a20 commit 2ededf1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ use futures_util::select;
use futures_util::stream::Stream;
use futures_util::StreamExt;

use std::cmp;
use std::collections::HashMap;
use std::iter;
use std::net::{SocketAddr, ToSocketAddrs};
Expand All @@ -116,10 +117,12 @@ use std::str::{self, FromStr};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use subslice::SubsliceExt;
use tokio::io::ErrorKind;
use tokio::io::{AsyncRead, AsyncWriteExt};
use tokio::io::{AsyncReadExt, AsyncWrite};
use tokio::time::sleep;
use url::Url;

use bytes::{Buf, Bytes, BytesMut};
Expand Down Expand Up @@ -480,10 +483,16 @@ pub(crate) struct Connector {

impl Connector {
pub(crate) async fn connect(&mut self) -> Result<(ServerInfo, Connection), io::Error> {
for _ in 0..128 {
for i in 0..128 {
if let Ok(inner) = self.try_connect().await {
return Ok(inner);
}

let exp: u32 = i.try_into().unwrap_or(std::u32::MAX);
let max = Duration::from_secs(4);
let duration = cmp::min(Duration::from_millis(2_u64.saturating_pow(exp)), max);

sleep(duration).await;
}

Err(io::Error::new(io::ErrorKind::Other, "unable to connect"))
Expand Down

0 comments on commit 2ededf1

Please sign in to comment.