跳到主要内容
本页尚未翻译

下面显示的是英文原文。

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:

OptionWhat it doesDefault
namethe name the server's monitoring lists the connection undernone
ping_intervalhow often the client checks that the server still answers60 s
max_reconnectshow many failed reconnects in a row the client makes before it gives upno limit
connection_timeouthow long the connect at startup waits5 s
subscription_capacityhow many messages a subscription buffers before the client drops them as a slow consumer65 536
no_echothe connection's own publishes skip its own subscriptionsoff
custom_inbox_prefixthe 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.

/// 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);
},
)
}