Expand description
RabbitMQ over AMQP 0.9.1 for RustStream
services, backed by the lapin client.
A subscription consumes one queue, and a publish is one message on an exchange under a routing key. A queue is destructive storage: an acknowledged delivery leaves it. Acknowledgements, requeues and dead-letter routes are protocol frames, so the framework settles a message on the server and keeps no copy in the service.
§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_lapin::prelude::*;
use serde::Deserialize;
#[derive(Deserialize)]
struct Order {
id: u64,
}
#[subscriber("orders")]
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(
LapinBroker::new("amqp://localhost:5672").prefetch(nonzero!(64)),
|b| {
b.include(handle);
},
)
}cargo run -- run starts it. LapinBroker::new only records the configuration; the runtime
connects the broker at startup and shuts it down when the service stops. The queue orders has
to exist, or the broker declares it under
declare_topology(true).
§Where things are
The guides take a RabbitMQ service from installation to production, in this order:
getting started, work queues, exchanges and routing,
topology, acknowledgements, retries and dead letters,
reliable publishing, request and reply, testing and
running in production. The architecture pages show how the crate
sits between lapin 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
- LapinBroker:
LapinBroker, connected intoConnectedLapinBrokerand closed intoClosedLapinBroker. - Connection: the URI and
LapinBroker::connection_name. - TLS:
amqps://URIs with thetls-native-tls,tls-rustlsortls-rustls-ringfeature. - declare_topology:
LapinBroker::declare_topologydeclares what the descriptors state. - Shutdown: handles that outlive the connection report
AmqpError::Closed. - Prelude:
prelude, the one glob aRabbitMQservice imports. - Errors:
AmqpError.
§Subscriptions
- Subscriptions: a bare queue name, consumed as a classic queue.
- RabbitQueue:
RabbitQueue, a classic queue and the settings both descriptors share. - RabbitQuorumQueue:
RabbitQuorumQueue, a replicated queue that counts deliveries. - prefetch:
LapinBroker::prefetchandRabbitQueue::prefetchcap the deliveries in flight. - Arguments:
RabbitQueue::argumentpasses anyx-argument, withAMQPValueandFieldTable. - Batches: a slice handler over
LapinSubscriberbatches andRabbitQueue::batch_wait. - Lanes:
workers(n, by_key)keyed byPARTITION_KEY_HEADER.
§Exchanges
- RabbitExchange:
RabbitExchangeandRabbitQueue::bind, with the direct exchange and a plugin’s own type throughRabbitExchange::custom. - topic:
RabbitExchange::topic, routing on dotted patterns. - fanout:
RabbitExchange::fanout, a copy to every bound queue. - bind_with:
RabbitQueue::bind_withandRabbitExchange::headers, routing on headers. - consistent_hash:
RabbitExchange::consistent_hash, the consistent-hash plugin exchange (featureplugin-consistent-hash).
§Settlement
- Settlement: what ack, retry and drop send, on
LapinMessage. - dead_letter_exchange:
RabbitQueue::dead_letter_exchangeandRabbitQueue::dead_letter_routing_key. - Retries:
max_attemptsanddead_letteron a classic queue, counted by the runtime. - delivery_limit:
RabbitQuorumQueue::delivery_limit, the quorum queue’s own cap. - retry_after: a delayed redelivery held by the runtime.
- delay:
RabbitQueue::delaywithDelay::dlx_ttl, a waiting queue on the server. - plugin_dme:
Delay::plugin_dme, the delayed-message exchange (featureplugin-dme).
§Context
- AmqpContext:
AmqpContextand the keys incontext::keys: exchange, routing key, redelivered flag and delivery tag.
§Publishing
- LapinPublish:
LapinPublish, the plain publish policy, and its liveLapinPublisher. - exchange:
LapinPublish::exchange, the exchange a policy publishes to. - Headers: the headers that ride native AMQP properties.
- Properties:
LapinPublishStepsandLapinPublishOptionsset one message’s properties. - priority, expiration and persistent: the three
properties, read back through
PRIORITY_HEADERandEXPIRATION_HEADER.
§Transactions
- Transactions: the borrowed transaction on both transactional policies.
- ConfirmsPublish:
ConfirmsPublishand its liveConfirmsPublisher. - ServerTxPublish:
ServerTxPublishand its liveServerTxPublisher. - ConfirmsTransaction:
ConfirmsTransaction, an owned transaction.
§Request and reply
- LapinRequest:
LapinRequestand its liveLapinRequesterover direct reply-to. - DirectReplyTo:
DirectReplyTo, the responder’s reply transform.
§Tooling
- AsyncAPI: the AMQP bindings of the generated document (feature
asyncapi): server, channels, operations and the extension for bindings. - Testing: the in-process transport
TestAppconnects the broker to (featuretesting), and Live runs against a running server.
§Cargo features
asyncapi contributes the AMQP bindings to the generated document, and testing adds the
in-process mode TestApp::start connects the broker through. tls-native-tls, tls-rustls and
tls-rustls-ring map onto lapin’s own TLS backends. plugin-consistent-hash and plugin-dme
add the typed forms of two server plugins, which must be enabled on the server.
Re-exports§
pub use queue::QueueDescriptor;pub use queue::RabbitQueue;pub use queue::RabbitQuorumQueue;
Modules§
- context
- Per-delivery context fields exposed to handlers.
- prelude
- The imports a service on
RabbitMQwrites every time, in one glob.
Structs§
- Closed
Lapin Broker - The terminal witness returned by shutting down a
ConnectedLapinBroker. - Confirms
Publish - The confirm-transactional publish policy: same options as
LapinPublish, pairing intoConfirmsPublisher. - Confirms
Publisher - The live publisher that awaits broker confirms for every message.
- Confirms
Transaction - An owned confirm-transaction, opened by
transactionon aConfirmsPublisher. - Connected
Lapin Broker - The typed witness that
Broker::connectsucceeded: holds the live connection. - Direct
Reply To - Redirects each reply of a
#[subscriber(.., reply(..))]handler to the requester’s private reply-to address, echoing its correlation id. - Field
Table - A Map<String, AMQPValue>
- Lapin
Broker - A
RabbitMQbroker backed bylapin: configuration captured, no I/O performed yet. - Lapin
Message - One AMQP delivery, settled with the protocol’s native acknowledgement frames.
- Lapin
Publish - The fire-and-forget publish policy: pure declaration, constructible anywhere.
- Lapin
Publish Options - The AMQP basic properties one publish may differ from the next in.
- Lapin
Publisher - The live fire-and-forget publisher, on the connection’s shared publish channel. Cheap to clone.
- Lapin
Request - The request/reply policy: pure declaration, constructible anywhere, pairing into
LapinRequester. - Lapin
Requester - The live request/reply client over
RabbitMQdirect reply-to. - Lapin
Subscriber - A consumer on one queue, yielding
LapinMessagedeliveries. - Rabbit
Exchange - Describes the exchange side of a
RabbitQueuebinding. - Rabbit
Queue - Describes one classic-queue subscription: the queue, its expected settings, and its bindings.
- Rabbit
Quorum Queue - Describes one quorum-queue subscription: the queue, its expected settings, and its bindings.
- Server
TxPublish - The server-transactional publish policy: same options as
LapinPublish, pairing intoServerTxPublisher. - Server
TxPublisher - The live publisher backed by AMQP server transactions (
tx.select/tx.commit/tx.rollback).
Enums§
- AMQP
Value - Enumeration referencing the possible AMQP values depending on the types
- Amqp
Error - Errors returned by
LapinBrokerand the types it hands out. - Delay
- How a subscription handles
retry_after/nack_afterdelays.
Constants§
- EXPIRATION_
HEADER - Header carrying the AMQP per-message
expirationproperty of a DELIVERY: whole milliseconds, as decimal digits, which is how AMQP spells a TTL. - PARTITION_
KEY_ HEADER - Header carrying a message’s partition key, read by
Partitionedfor keyed worker lanes. - PRIORITY_
HEADER - Header carrying the AMQP
priorityproperty of a DELIVERY.
Traits§
- Lapin
Publish Policy - A publish policy that pairs with a connected
RabbitMQbroker without opening a channel. - Lapin
Publish Steps - The AMQP basic properties, as steps on the publish builder.
- Queue
Descriptor - The queue a subscription consumes:
RabbitQueueorRabbitQuorumQueue.