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

Proposed update to enable reconnect on first attempt #223

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/asynk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,28 @@ impl Options {
}
}

/// Select option to enable reconnect with backoff
/// on first failed connection attempt.
/// The reconnect logic with `max_reconnects` and the
/// `reconnect_delay_callback` will be specified the same
/// as before but will be invoked on the first failed
/// connection attempt.
///
/// # Example
/// ```
/// # smol::block_on(async {
/// let nc = nats::asynk::Options::new()
/// .retry_on_failed_connect()
/// .connect("demo.nats.io")
/// .await?;
/// # std::io::Result::Ok(()) });
/// ```
pub fn retry_on_failed_connect(self) -> Options {
Options {
inner: self.inner.retry_on_failed_connect(),
}
}

/// Set the maximum number of reconnect attempts.
/// If no servers remain that are under this threshold,
/// then no further reconnect shall be attempted.
Expand Down
5 changes: 3 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,9 @@ impl Client {
let mut first_connect = true;

loop {
// Don't use backoff on first connect.
let use_backoff = !first_connect;
// Don't use backoff on first connect unless retry_on_failed_connect is set to true.
let use_backoff = self.options.retry_on_failed_connect || !first_connect;

// Make a connection to the server.
let (server_info, stream) = connector.connect(use_backoff)?;

Expand Down
24 changes: 24 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Options {
pub(crate) auth: AuthStyle,
pub(crate) name: Option<String>,
pub(crate) no_echo: bool,
pub(crate) retry_on_failed_connect: bool,
pub(crate) max_reconnects: Option<usize>,
pub(crate) reconnect_buffer_size: usize,
pub(crate) tls_required: bool,
Expand All @@ -36,6 +37,7 @@ impl fmt::Debug for Options {
.entry(&"auth", &self.auth)
.entry(&"name", &self.name)
.entry(&"no_echo", &self.no_echo)
.entry(&"retry_on_failed_connect", &self.retry_on_failed_connect)
.entry(&"reconnect_buffer_size", &self.reconnect_buffer_size)
.entry(&"max_reconnects", &self.max_reconnects)
.entry(&"tls_required", &self.tls_required)
Expand All @@ -57,6 +59,7 @@ impl Default for Options {
auth: AuthStyle::NoAuth,
name: None,
no_echo: false,
retry_on_failed_connect: false,
reconnect_buffer_size: 8 * 1024 * 1024,
max_reconnects: Some(60),
tls_required: false,
Expand Down Expand Up @@ -383,6 +386,27 @@ impl Options {
self
}

/// Select option to enable reconnect with backoff
/// on first failed connection attempt.
/// The reconnect logic with `max_reconnects` and the
/// `reconnect_delay_callback` will be specified the same
/// as before but will be invoked on the first failed
/// connection attempt.
///
/// # Example
Jarema marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// # fn main() -> std::io::Result<()> {
/// let nc = nats::Options::new()
/// .retry_on_failed_connect()
/// .connect("demo.nats.io")?;
/// # Ok(())
/// # }
/// ```
pub fn retry_on_failed_connect(mut self) -> Options {
self.retry_on_failed_connect = true;
self
}

/// Set the maximum number of reconnect attempts.
/// If no servers remain that are under this threshold,
/// then no further reconnect shall be attempted.
Expand Down