1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
//! A Rust client for the NATS.io ecosystem. //! //! `git clone https://github.com/nats-io/nats.rs` //! //! NATS.io is a simple, secure and high performance open source messaging system for cloud native //! applications, `IoT` messaging, and microservices architectures. //! //! For more information see [https://nats.io/]. //! //! [https://nats.io/]: https://nats.io/ //! //! ## Examples //! //! `> cargo run --example nats-box -- -h` //! //! Basic connections, and those with options. The compiler will force these to be correct. //! //! ```no_run //! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! let nc = nats::connect("demo.nats.io")?; //! //! let nc2 = nats::Options::with_user_pass("derek", "s3cr3t!") //! .with_name("My Rust NATS App") //! .connect("127.0.0.1")?; //! //! let nc3 = nats::Options::with_credentials("path/to/my.creds") //! .connect("connect.ngs.global")?; //! //! let nc4 = nats::Options::new() //! .add_root_certificate("my-certs.pem") //! .connect("tls://demo.nats.io:4443")?; //! # Ok(()) } //! ``` //! //! ### Publish //! //! ``` //! # fn main() -> std::io::Result<()> { //! let nc = nats::connect("demo.nats.io")?; //! nc.publish("my.subject", "Hello World!")?; //! //! nc.publish("my.subject", "my message")?; //! //! // Publish a request manually. //! let reply = nc.new_inbox(); //! let rsub = nc.subscribe(&reply)?; //! nc.publish_request("my.subject", &reply, "Help me!")?; //! # Ok(()) } //! ``` //! //! ### Subscribe //! //! ```no_run //! # fn main() -> std::io::Result<()> { //! # use std::time::Duration; //! let nc = nats::connect("demo.nats.io")?; //! let sub = nc.subscribe("foo")?; //! for msg in sub.messages() {} //! //! // Using next. //! if let Some(msg) = sub.next() {} //! //! // Other iterators. //! for msg in sub.try_iter() {} //! for msg in sub.timeout_iter(Duration::from_secs(10)) {} //! //! // Using a threaded handler. //! let sub = nc.subscribe("bar")?.with_handler(move |msg| { //! println!("Received {}", &msg); //! Ok(()) //! }); //! //! // Queue subscription. //! let qsub = nc.queue_subscribe("foo", "my_group")?; //! # Ok(()) } //! ``` //! //! ### Request/Response //! //! ```no_run //! # use std::time::Duration; //! # fn main() -> std::io::Result<()> { //! let nc = nats::connect("demo.nats.io")?; //! let resp = nc.request("foo", "Help me?")?; //! //! // With a timeout. //! let resp = nc.request_timeout("foo", "Help me?", Duration::from_secs(2))?; //! //! // With multiple responses. //! for msg in nc.request_multi("foo", "Help")?.iter() {} //! //! // Publish a request manually. //! let reply = nc.new_inbox(); //! let rsub = nc.subscribe(&reply)?; //! nc.publish_request("foo", &reply, "Help me!")?; //! let response = rsub.iter().take(1); //! # Ok(()) } //! ``` #![cfg_attr(test, deny(warnings))] #![deny( missing_docs, future_incompatible, nonstandard_style, rust_2018_idioms, missing_copy_implementations, trivial_casts, trivial_numeric_casts, unsafe_code, unused_qualifications )] #![deny( // over time, consider enabling the following commented-out lints: // clippy::else_if_without_else, // clippy::indexing_slicing, // clippy::multiple_crate_versions, // clippy::missing_const_for_fn, clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_precision_loss, clippy::cast_sign_loss, clippy::checked_conversions, clippy::decimal_literal_representation, clippy::doc_markdown, clippy::empty_enum, clippy::explicit_into_iter_loop, clippy::explicit_iter_loop, clippy::expl_impl_clone_on_copy, clippy::fallible_impl_from, clippy::filter_map, clippy::filter_map_next, clippy::find_map, clippy::float_arithmetic, clippy::get_unwrap, clippy::if_not_else, clippy::inline_always, clippy::invalid_upcast_comparisons, clippy::items_after_statements, clippy::map_flatten, clippy::map_unwrap_or, clippy::match_same_arms, clippy::maybe_infinite_iter, clippy::mem_forget, clippy::module_name_repetitions, clippy::multiple_inherent_impl, clippy::needless_borrow, clippy::needless_continue, clippy::needless_pass_by_value, clippy::non_ascii_literal, clippy::path_buf_push_overwrite, clippy::print_stdout, clippy::pub_enum_variant_names, clippy::shadow_reuse, clippy::shadow_same, clippy::shadow_unrelated, clippy::single_match_else, clippy::string_add, clippy::string_add_assign, clippy::type_repetition_in_bounds, clippy::unicode_not_nfc, clippy::unimplemented, clippy::unseparated_literal_suffix, clippy::wildcard_dependencies, clippy::wildcard_enum_match_arm, clippy::wrong_pub_self_convention, )] #![allow(clippy::match_like_matches_macro)] use crate::asynk::client::Client; use crate::smol::{future, prelude::*, Timer}; pub mod asynk; mod auth_utils; mod connect; mod headers; mod options; mod secure_wipe; /// A subset of the smol runtime. /// /// We're only using a subset because async-process requires Rust 1.45, but our minimum required /// Rust version is older than that. pub(crate) mod smol { pub use async_executor::*; pub use async_io::*; pub use futures_lite::*; pub use async_channel as channel; pub use async_lock as lock; pub use async_net as net; } #[cfg(feature = "fault_injection")] mod fault_injection; #[cfg(feature = "fault_injection")] use fault_injection::{inject_delay, inject_io_failure}; #[cfg(not(feature = "fault_injection"))] async fn inject_delay() {} #[cfg(not(feature = "fault_injection"))] fn inject_io_failure() -> io::Result<()> { Ok(()) } /// Functionality relating to subscribing to a /// subject. pub mod subscription; #[doc(hidden)] #[deprecated(since = "0.6.0", note = "this has been renamed to `Options`.")] pub type ConnectionOptions = Options; use std::{ fmt, io::{self, Error, ErrorKind}, sync::Arc, time::Duration, }; pub use {headers::Headers, options::Options, subscription::Subscription}; #[doc(hidden)] pub use connect::ConnectInfo; use secure_wipe::{SecureString, SecureVec}; const VERSION: &str = env!("CARGO_PKG_VERSION"); const LANG: &str = "rust"; /// Information sent by the server back to this client /// during initial connection, and possibly again later. #[derive(Debug, Clone)] struct ServerInfo { /// The unique identifier of the NATS server. pub server_id: String, /// Generated Server Name. pub server_name: String, /// The host specified in the cluster parameter/options. pub host: String, /// The port number specified in the cluster parameter/options. pub port: i16, /// The version of the NATS server. pub version: String, /// If this is set, then the server should try to authenticate upon connect. pub auth_required: bool, /// If this is set, then the server must authenticate using TLS. pub tls_required: bool, /// Maximum payload size that the server will accept. pub max_payload: i32, /// The protocol version in use. pub proto: i8, /// The server-assigned client ID. This may change during reconnection. pub client_id: u64, /// The version of golang the NATS server was built with. pub go: String, /// The nonce used for nkeys. pub nonce: String, /// A list of server urls that a client can connect to. pub connect_urls: Vec<String>, /// The client IP as known by the server. pub client_ip: String, } impl ServerInfo { fn parse(s: &str) -> Option<ServerInfo> { let mut obj = json::parse(s).ok()?; Some(ServerInfo { server_id: obj["server_id"].take_string()?, server_name: obj["server_name"].take_string().unwrap_or_default(), host: obj["host"].take_string()?, port: obj["port"].as_i16()?, version: obj["version"].take_string()?, auth_required: obj["auth_required"].as_bool().unwrap_or(false), tls_required: obj["tls_required"].as_bool().unwrap_or(false), max_payload: obj["max_payload"].as_i32()?, proto: obj["proto"].as_i8()?, client_id: obj["client_id"].as_u64()?, go: obj["go"].take_string()?, nonce: obj["nonce"].take_string().unwrap_or_default(), connect_urls: obj["connect_urls"] .members_mut() .filter_map(|m| m.take_string()) .collect(), client_ip: obj["client_ip"].take_string().unwrap_or_default(), }) } } use options::AuthStyle; /// A NATS connection. #[derive(Debug, Clone)] pub struct Connection(asynk::Connection); /// Connect to a NATS server at the given url. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// let nc = nats::connect("demo.nats.io")?; /// # Ok(()) /// # } /// ``` pub fn connect(nats_url: &str) -> io::Result<Connection> { Options::new().connect(nats_url) } /// A `Message` that has been published to a NATS `Subject`. #[derive(Debug, Clone)] pub struct Message { /// The NATS `Subject` that this `Message` has been published to. pub subject: String, /// The optional reply `Subject` that may be used for sending /// responses when using the request/reply pattern. pub reply: Option<String>, /// The `Message` contents. pub data: Vec<u8>, /// Client for publishing on the reply subject. pub(crate) client: Client, /// Optional headers associated with this `Message`. pub headers: Option<Headers>, } impl Message { pub(crate) fn from_async(msg: asynk::Message) -> Message { Message { subject: msg.subject, reply: msg.reply, data: msg.data, client: msg.client, headers: msg.headers, } } /// Respond to a request message. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// nc.subscribe("help.request")?.with_handler(move |m| { /// m.respond("ans=42")?; Ok(()) /// }); /// # Ok(()) /// # } /// ``` pub fn respond(&self, msg: impl AsRef<[u8]>) -> io::Result<()> { match self.reply.as_ref() { None => Err(Error::new( ErrorKind::InvalidInput, "no reply subject available", )), Some(reply) => future::block_on(self.client.publish(reply, None, None, msg.as_ref())), } } } impl fmt::Display for Message { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut body = format!("[{} bytes]", self.data.len()); if let Ok(str) = std::str::from_utf8(&self.data) { body = str.to_string(); } if let Some(reply) = &self.reply { write!( f, "Message {{\n subject: \"{}\",\n reply: \"{}\",\n data: \"{}\"\n}}", self.subject, reply, body ) } else { write!( f, "Message {{\n subject: \"{}\",\n data: \"{}\"\n}}", self.subject, body ) } } } impl Connection { /// Create a subscription for the given NATS connection. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let sub = nc.subscribe("foo")?; /// # Ok(()) /// # } /// ``` pub fn subscribe(&self, subject: &str) -> io::Result<Subscription> { future::block_on(self.0.subscribe(subject)).map(|s| Subscription(Arc::new(s))) } /// Create a queue subscription for the given NATS connection. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let sub = nc.queue_subscribe("foo", "production")?; /// # Ok(()) /// # } /// ``` pub fn queue_subscribe(&self, subject: &str, queue: &str) -> io::Result<Subscription> { future::block_on(self.0.queue_subscribe(subject, queue)).map(|s| Subscription(Arc::new(s))) } /// Publish a message on the given subject. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// nc.publish("foo", "Hello World!")?; /// # Ok(()) /// # } /// ``` pub fn publish(&self, subject: &str, msg: impl AsRef<[u8]>) -> io::Result<()> { future::block_on(self.0.publish(subject, msg)) } /// Publish a message on the given subject with a reply subject for responses. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let reply = nc.new_inbox(); /// let rsub = nc.subscribe(&reply)?; /// nc.publish_request("foo", &reply, "Help me!")?; /// # Ok(()) /// # } /// ``` pub fn publish_request( &self, subject: &str, reply: &str, msg: impl AsRef<[u8]>, ) -> io::Result<()> { future::block_on(self.0.publish_request(subject, reply, msg)) } /// Create a new globally unique inbox which can be used for replies. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let reply = nc.new_inbox(); /// let rsub = nc.subscribe(&reply)?; /// # Ok(()) /// # } /// ``` pub fn new_inbox(&self) -> String { self.0.new_inbox() } /// Publish a message on the given subject as a request and receive the response. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// # nc.subscribe("foo")?.with_handler(move |m| { m.respond("ans=42")?; Ok(()) }); /// let resp = nc.request("foo", "Help me?")?; /// # Ok(()) /// # } /// ``` pub fn request(&self, subject: &str, msg: impl AsRef<[u8]>) -> io::Result<Message> { future::block_on(self.0.request(subject, msg)).map(Message::from_async) } /// Publish a message on the given subject as a request and receive the response. /// This call will return after the timeout duration if no response is received. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// # nc.subscribe("foo")?.with_handler(move |m| { m.respond("ans=42")?; Ok(()) }); /// let resp = nc.request_timeout("foo", "Help me?", std::time::Duration::from_secs(2))?; /// # Ok(()) /// # } /// ``` pub fn request_timeout( &self, subject: &str, msg: impl AsRef<[u8]>, timeout: Duration, ) -> io::Result<Message> { future::block_on(async { self.0 .request(subject, msg) .or(async { Timer::after(timeout).await; Err(ErrorKind::TimedOut.into()) }) .await .map(Message::from_async) }) } /// Publish a message on the given subject as a request and allow multiple responses. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// # nc.subscribe("foo")?.with_handler(move |m| { m.respond("ans=42")?; Ok(()) }); /// for msg in nc.request_multi("foo", "Help")?.iter().take(1) {} /// # Ok(()) /// # } /// ``` pub fn request_multi(&self, subject: &str, msg: impl AsRef<[u8]>) -> io::Result<Subscription> { future::block_on(self.0.request_multi(subject, msg)).map(|s| Subscription(Arc::new(s))) } /// Flush a NATS connection by sending a `PING` protocol and waiting for the responding `PONG`. /// Will fail with `TimedOut` if the server does not respond with in 10 seconds. /// Will fail with `NotConnected` if the server is not currently connected. /// Will fail with `BrokenPipe` if the connection to the server is lost. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// nc.flush()?; /// # Ok(()) /// # } /// ``` pub fn flush(&self) -> io::Result<()> { future::block_on(self.0.flush()) } /// Flush a NATS connection by sending a `PING` protocol and waiting for the responding `PONG`. /// Will fail with `TimedOut` if the server takes longer than this duration to respond. /// Will fail with `NotConnected` if the server is not currently connected. /// Will fail with `BrokenPipe` if the connection to the server is lost. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// nc.flush()?; /// # Ok(()) /// # } /// ``` pub fn flush_timeout(&self, duration: Duration) -> io::Result<()> { future::block_on(self.0.flush_timeout(duration)) } /// Close a NATS connection. All clones of /// this `Connection` will also be closed, /// as the backing IO threads are shared. /// /// If the client is currently connected /// to a server, the outbound write buffer /// will be flushed in the process of /// shutting down. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// nc.close(); /// # Ok(()) /// # } /// ``` pub fn close(self) { future::block_on(self.0.close()).ok(); } /// Calculates the round trip time between this client and the server, /// if the server is currently connected. Fails with `TimedOut` if /// the server takes more than 10 seconds to respond. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// println!("server rtt: {:?}", nc.rtt()); /// # Ok(()) /// # } /// ``` pub fn rtt(&self) -> io::Result<Duration> { future::block_on(self.0.rtt()) } /// Returns the client IP as known by the server. /// Supported as of server version 2.1.6. /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// println!("ip: {:?}", nc.client_ip()); /// # Ok(()) /// # } /// ``` pub fn client_ip(&self) -> io::Result<std::net::IpAddr> { self.0.client_ip() } /// Returns the client ID as known by the most recently connected server. /// /// # Example /// ``` /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// println!("ip: {:?}", nc.client_id()); /// # Ok(()) /// # } /// ``` pub fn client_id(&self) -> u64 { self.0.client_id() } /// Send an unsubscription for all subs then flush the connection, allowing any unprocessed /// messages to be handled by a handler function if one is configured. /// /// After the flush returns, we know that a round-trip to the server has happened after it /// received our unsubscription, so we shut down the subscriber afterwards. /// /// A similar method exists for the `Subscription` struct which will drain /// a single `Subscription` without shutting down the entire connection /// afterward. /// /// # Example /// ``` /// # use std::sync::{Arc, atomic::{AtomicBool, Ordering::SeqCst}}; /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let received = Arc::new(AtomicBool::new(false)); /// let received_2 = received.clone(); /// /// nc.subscribe("test.drain")?.with_handler(move |m| { /// received_2.store(true, SeqCst); /// Ok(()) /// }); /// /// nc.publish("test.drain", "message")?; /// nc.drain()?; /// /// # std::thread::sleep(std::time::Duration::from_secs(1)); /// /// assert!(received.load(SeqCst)); /// /// # Ok(()) /// # } /// ``` pub fn drain(&self) -> io::Result<()> { future::block_on(self.0.drain()) } /// Publish a message which may have a reply subject or headers set. /// /// # Example /// ```no_run /// # fn main() -> std::io::Result<()> { /// # let nc = nats::connect("demo.nats.io")?; /// let sub = nc.subscribe("foo.headers")?; /// let headers = [("header1", "value1"), /// ("header2", "value2")].iter().collect(); /// let reply_to = None; /// nc.publish_with_reply_or_headers("foo.headers", reply_to, Some(&headers), "Hello World!")?; /// nc.flush()?; /// let message = sub.next_timeout(std::time::Duration::from_secs(2)).unwrap(); /// assert_eq!(message.headers.unwrap().len(), 2); /// # Ok(()) /// # } /// ``` pub fn publish_with_reply_or_headers( &self, subject: &str, reply: Option<&str>, headers: Option<&Headers>, msg: impl AsRef<[u8]>, ) -> io::Result<()> { future::block_on(self.0.publish_with_reply_or_headers( subject, reply, headers, msg.as_ref(), )) } }