Skip to content

Commit

Permalink
nats-io#1186 Configure max_reconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
glueball authored and jaumelopez committed Dec 31, 2023
1 parent dced391 commit c15a15a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions async-nats/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,32 @@ impl ConnectOptions {
self
}

/// Specifies the number of consecutive reconnect attempts the client will
/// make before giving up. This is useful for preventing zombie services
/// from endlessly reaching the servers, but it can also be a footgun and
/// surprise for users who do not expect that the client can give up
/// entirely.
///
/// Pass `None` or `0` for no limit.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// async_nats::ConnectOptions::new()
/// .max_reconnects(None)
/// .connect("demo.nats.io")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn max_reconnects<T: Into<Option<usize>>>(mut self, max_reconnects: T) -> ConnectOptions {
let val: Option<usize> = max_reconnects.into();
self.max_reconnects = if val == Some(0) { None } else { val };
self
}

pub fn ignore_discovered_servers(mut self) -> ConnectOptions {
self.ignore_discovered_servers = true;
self
Expand Down

0 comments on commit c15a15a

Please sign in to comment.