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

Improve reconnect randomization #868

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions async-nats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lazy_static = "1.4"
base64 = "0.13"
tokio-retry = "0.3"
ring = "0.16"
rand = "0.8"

[dev-dependencies]
criterion = { version = "0.3", features = ["async_tokio"]}
Expand Down
12 changes: 9 additions & 3 deletions async-nats/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use crate::ToServerAddrs;
use crate::LANG;
use crate::VERSION;
use bytes::BytesMut;
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::cmp;
use std::collections::HashMap;
use std::io;
Expand Down Expand Up @@ -97,8 +99,13 @@ impl Connector {

pub(crate) async fn try_connect(&mut self) -> Result<(ServerInfo, Connection), ConnectError> {
let mut error = None;
let server_addrs = {
let mut rng = thread_rng();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already mut, so connector could own the rng without issue.

Suggested change
let mut rng = thread_rng();
let mut rng = thread_rng();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure.
Connector needs to be Send.

Do we prefer to just call it at try_connect or put it behind Arc?
I think the former.

let mut server_addrs: Vec<ServerAddr> = self.servers.keys().cloned().collect();
server_addrs.shuffle(&mut rng);
server_addrs
};

let server_addrs: Vec<ServerAddr> = self.servers.keys().cloned().collect();
for server_addr in server_addrs {
let server_attempts = self.servers.get_mut(&server_addr).unwrap();
let duration = if *server_attempts == 0 {
Expand All @@ -109,7 +116,6 @@ impl Connector {

cmp::min(Duration::from_millis(2_u64.saturating_pow(exp)), max)
};

*server_attempts += 1;
sleep(duration).await;

Expand Down Expand Up @@ -227,8 +233,8 @@ impl Connector {
}
},
Some(_) => {
self.events_tx.send(Event::Connected).await.ok();
self.state_tx.send(State::Connected).ok();
self.events_tx.send(Event::Connected).await.ok();
return Ok((server_info, connection));
}
None => {
Expand Down
2 changes: 0 additions & 2 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,6 @@ impl ConnectionHandler {
.await
.unwrap();
}
self.connector.events_tx.try_send(Event::Connected).ok();

Ok(())
}
}
Expand Down
42 changes: 42 additions & 0 deletions async-nats/tests/jwt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,48 @@ mod client {
.await
.expect("published");
}

#[cfg(not(target_os = "windows"))]
#[tokio::test]
async fn jwt_lame_duck_reconnect() {
use async_nats::Event;
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let c = nats_server::run_cluster("tests/configs/jwt.conf");

let (tx_recconect, mut rx_reconnect) = tokio::sync::mpsc::channel(10);

let client = async_nats::ConnectOptions::with_credentials_file(
path.join("tests/configs/TestUser.creds"),
)
.await
.unwrap()
.event_callback({
let tx = tx_recconect.clone();
move |event| {
let tx = tx.clone();
async move {
if event == Event::Connected {
tx.send(()).await.unwrap();
}
}
}
})
.connect(c.client_url())
.await
.unwrap();

let mut subscriber = client.subscribe("test".into()).await.unwrap();
for i in 0..2 {
rx_reconnect.recv().await;
let mut subscribe = client.subscribe("test".into()).await.unwrap();
client.publish("test".into(), "data".into()).await.unwrap();
subscribe.next().await.unwrap();
client.flush().await.unwrap();
assert!(subscriber.next().await.is_some());
nats_server::set_lame_duck_mode(&c.servers[i]);
}
}

#[tokio::test]
async fn jwt_reconnect() {
use async_nats::ServerAddr;
Expand Down