Ниже показан английский оригинал.
ConnectOptions
with_options attaches async_nats::ConnectOptions to the broker: the client's name, pings, reconnects, no_echo and the inbox prefix.
ConnectOptions is the builder of the async-nats client this crate runs on.
Building it does no I/O, and the broker applies it when the runtime connects.
A service that names it adds async-nats to its own dependencies, in the version this crate uses.
An option the service leaves unset keeps the client's default.
The options a service sets most often:
| Option | What it does | Default |
|---|---|---|
name | the name the server's monitoring lists the connection under | none |
ping_interval | how often the client checks that the server still answers | 60 s |
max_reconnects | how many failed reconnects in a row the client makes before it gives up | no limit |
connection_timeout | how long the connect at startup waits | 5 s |
subscription_capacity | how many messages a subscription buffers before the client drops them as a slow consumer | 65 536 |
no_echo | the connection's own publishes skip its own subscriptions | off |
custom_inbox_prefix | the subject prefix of the inboxes requests are answered on | _INBOX |
Credentials and TLS are options too, on the Security page.
no_echo and the inbox prefix change what the server delivers, so the in-process mode of a TestApp test reads them as the server would.
With no_echo a reply or a publish of the service never reaches a handler of the same service, while a message from another connection does.
The other options shape the client's own connection, which the in-process mode has none of.
Example
The dependency line:
async-nats = "0.50"
The service names its connection pricing-svc and bounds its reconnects.
It publishes cache invalidations to a subject it also reads, and no_echo keeps its own invalidations from coming back to it.
- Macros
- Manual
/// The servers at `addrs`, with the client settings this service relies on.
fn broker(addrs: &str) -> NatsBroker {
NatsBroker::new(addrs).with_options(
ConnectOptions::new()
// The name the server's monitoring lists this connection under.
.name("pricing-svc")
.ping_interval(Duration::from_secs(20))
// Give up after ten failed reconnects in a row.
.max_reconnects(10)
// This connection's own publishes skip its own subscriptions.
.no_echo()
.custom_inbox_prefix("_INBOX_pricing"),
)
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("pricing", "0.1.0")).with_broker(
broker("nats://localhost:4222"),
|b| {
b.include(reprice);
b.include(evict);
},
)
}
/// The servers at `addrs`, with the client settings this service relies on.
fn broker(addrs: &str) -> NatsBroker {
NatsBroker::new(addrs).with_options(
ConnectOptions::new()
// The name the server's monitoring lists this connection under.
.name("pricing-svc")
.ping_interval(Duration::from_secs(20))
// Give up after ten failed reconnects in a row.
.max_reconnects(10)
// This connection's own publishes skip its own subscriptions.
.no_echo()
.custom_inbox_prefix("_INBOX_pricing"),
)
}
fn app() -> impl App {
RustStream::new(AppInfo::new("pricing", "0.1.0")).with_broker(
broker("nats://localhost:4222"),
|b| {
b.include(subscriber("prices.updated", Reprice).reply().build());
b.include(subscriber("cache.invalidate", Evict).build());
},
)
}