Skip to content

Commit

Permalink
Add examples for connect with multiple urls
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Jan 13, 2023
1 parent 743458a commit ec28bec
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
52 changes: 52 additions & 0 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@ impl fmt::Display for Event {
/// To have customized NATS connection, check [ConnectOptions].
///
/// # Examples
///
/// ## Single URL
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
Expand All @@ -768,6 +770,56 @@ impl fmt::Display for Event {
/// # Ok(())
/// # }
/// ```
///
/// ## Connect with [Vec] of [ServerAddr].
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(vec![
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
///])
///.await
///.unwrap();
///# Ok(())
///# }
/// ```
///
/// ## with [Vec], but parse URLs inside [crate::connect()]
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let servers = vec!["demo.nats.io", "other.nats.io"];
///let client = async_nats::connect(
/// servers
/// .iter()
/// .map(|url| url.parse())
/// .collect::<Result<Vec<ServerAddr>, _>>()?
///)
///.await?;
///# Ok(())
///# }
///```
///
///
/// ## with slice.
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(
/// [
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
/// ]
/// .as_slice(),
///)
///.await?;
///# Ok(())
///# }
///
pub async fn connect<A: ToServerAddrs>(addrs: A) -> Result<Client, io::Error> {
connect_with_options(addrs, ConnectOptions::default()).await
}
Expand Down
15 changes: 15 additions & 0 deletions async-nats/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ impl ConnectOptions {
/// # Ok(())
/// # }
/// ```
///
/// ## Pass multiple URLs.
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(vec![
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
///])
///.await
///.unwrap();
///# Ok(())
///# }
/// ```
pub async fn connect<A: ToServerAddrs>(self, addrs: A) -> io::Result<Client> {
crate::connect_with_options(addrs, self).await
}
Expand Down

0 comments on commit ec28bec

Please sign in to comment.