Expand description
NATS broker for the RustStream messaging framework,
backed by async-nats: Core NATS subjects and JetStream streams
over one connection.
Core NATS hands a message to whoever is subscribed at that moment and stores nothing. JetStream
stores it in a stream, which a pull consumer reads at its own pace and acknowledges message by
message. Both models run on the same connection, so a service chooses per handler and per publish.
§A service
A handler is an async fn over the decoded payload, mounted on a broker inside the application
object, and the attribute writes the main:
use ruststream_nats::prelude::*;
use serde::Deserialize;
#[derive(Deserialize)]
struct Order {
id: u64,
}
#[subscriber("orders.created")]
async fn handle(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "accepted");
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
NatsBroker::new("nats://localhost:4222"),
|b| {
b.include(handle);
},
)
}cargo run -- run starts it. NatsBroker::new only records the configuration; the runtime
connects the broker at startup and drains it when the service stops.
§Where things are
The guides take a NATS service from installation to production, in this order:
getting started, subjects, wildcards and queue groups,
streams and consumers, acknowledgement and delivery guarantees,
publishing to streams, retries and dead letters,
request-reply, testing and
connecting to production. The architecture pages show how the
crate sits between async-nats and the runtime, and the benchmarks what it costs.
Each topic below links to its component page on the site, which shows it in a service; the item’s own documentation keeps the detail.
§Broker
- NatsBroker:
NatsBroker, connected intoConnectedNatsBrokerand closed intoClosedNatsBroker. - ConnectOptions:
NatsBroker::with_optionsattaches theasync-natsconnection options. - Security: credentials and TLS as connection options.
- Startup: the connect within the client’s
connection_timeout, and each subscription opened against the server. - Shutdown: the drain, and
NatsError::Closedfor a publisher used afterwards. - Prelude:
prelude, the one glob a NATS service imports. - Errors:
NatsError.
§Subscriptions
- Subscriptions: a bare subject in
#[subscriber]reads that Core NATS subject. - CoreSubject:
CoreSubject, one Core NATS subject. - CoreWildcard:
CoreWildcard, every subject a*or>pattern matches. - queue_group:
CoreSubject::queue_group, one member of a group takes each message. - JetStreamSubject:
JetStreamSubject, a stream read through a pull consumer. - durable:
JetStreamSubject::durable, a named consumer that keeps its position. - filter_subject:
JetStreamSubject::filter_subject, the subjects of the stream a consumer reads. - DeliverPolicy:
DeliverPolicy, where a new consumer starts. - max_ack_pending:
JetStreamSubject::max_ack_pending, the cap on unacknowledged deliveries. - Batches: a slice handler over
NatsSubscriberbatches, sized at the mount site and timed byJetStreamSubject::pull_expires. - Keys:
PARTITION_KEY_HEADERkeys the lanes ofworkers(n, by_key).
§Settlement
- Acknowledgement: what an
ack, aretryand adropdo on aJetStreamdelivery. - ack_wait:
JetStreamSubject::ack_wait, how long the server waits for a settlement. - At most once: a Core NATS delivery, which nothing acknowledges.
- Retries:
max_attempts, counted by the server onJetStreamand in a header on Core NATS. - retry_after: a delay the server holds on
JetStream, and a copy the framework publishes on Core NATS. - Dead letters:
dead_letter, the subject a spent delivery goes to. - out_retry: the subject a
CoreWildcardregistration sends its delayed copies to.
§Context
- JetStreamContext:
JetStreamContextand the keys incontext::keys: stream, consumer, sequences, delivery count and pending count of a delivery.
§Publishing
- NatsPublish:
NatsPublish, the plain Core NATS policy, and its liveNatsPublisher. - Headers: what a header must be to travel over NATS.
- JetStreamPublish:
JetStreamPublishand its liveJetStreamPublisher, which wait for the stream’s acknowledgement. - expect_stream:
JetStreamPublish::expect_stream, the stream a publisher’s subjects must be stored by. - message_id:
JetStreamPublishSteps::message_id, a deduplication id carried inJetStreamOptions. - Expectations: the
expect_last_*steps ofJetStreamPublishSteps. - publish_ack:
JetStreamPublisher::publish_ackand itsPublishAck.
§Request-reply
- RequestReply:
requestonNatsPublisher, answered on an inbox. - Responders: a handler that answers on the inbox of each request.
§Tooling
- NATS bindings: the server,
channels, operations and
replies of the generated
AsyncAPIdocument (featureasyncapi). - Testing: the in-process NATS server
TestAppconnects the broker to (featuretesting), and Live runs against a running server.
§Cargo features
Both are off by default and additive. testing adds the in-process mode TestApp::start
connects the broker through, and asyncapi contributes the NATS bindings to the generated
document. The codecs, the macros and the harness itself are features of the core crate.
Modules§
- context
- Native
JetStreammetadata of a delivery, read by a handler throughCtx<K>. - prelude
- The imports a service on NATS writes every time, in one glob.
Structs§
- Closed
Nats Broker - The terminal witness returned by shutting down a
ConnectedNatsBroker. - Connected
Nats Broker - The typed witness that
Broker::connectsucceeded: holds the live connection. - Core
Message - Wrapper around an
async_nats::Messagefrom a core (non-JetStream) subscription. - Core
Subject - One subject subscribed to over Core NATS.
- Core
Wildcard - A pattern subscribed to over Core NATS:
orders.*,orders.>,>. - JetStream
Message - Wrapper around an
async_nats::jetstream::Messagewith ack semantics. - JetStream
Options - What one
JetStreampublish states about itself, over and above its subject and payload. - JetStream
Publish - The
JetStreampublish policy: pure declaration, constructible anywhere. - JetStream
Publisher - The live
JetStreampublisher. Cheap to clone. - JetStream
Subject - A subject read through a
JetStreampull consumer on one stream. - Nats
Broker - A NATS broker: configuration captured, no I/O performed yet.
- Nats
Publish - The Core NATS publish policy: pure declaration, constructible anywhere.
- Nats
Publisher - The live Core NATS publisher. Cheap to clone.
- Nats
Subscriber - A NATS subscription.
- NonZero
Duration - A
Durationthat is known not to be zero. - Publish
Ack - The acknowledgement a
JetStreamstream returns for an accepted publish.PublishAckis an acknowledgment received after successfully publishing a message.
Enums§
- Deliver
Policy DeliverPolicydetermines how the consumer should select the first message to deliver.- Nats
Error - Errors surfaced by the NATS broker implementation.
- Nats
Message - A NATS delivery. Two flavours: core NATS (no ack) and
JetStream(real ack/nack/redelivery).
Constants§
- PARTITION_
KEY_ HEADER - The well-known header key for per-message routing / partitioning.
Traits§
- JetStream
Publish Steps - The per-message
JetStreamsettings, as steps on the publish builder. - Nats
Publish Policy - A publish policy that pairs with a connected NATS broker without I/O.
- Nats
Subscription - A NATS subscription descriptor:
CoreSubject,CoreWildcardorJetStreamSubject.