Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleep between connection attempts #407

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -115,10 +116,12 @@ use std::slice;
use std::str::{self, FromStr};
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 @@ -479,10 +482,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