queuey_rabbitmq/lib.rs
1//! RabbitMQ backend for [`queuey`], built on [`lapin`].
2//!
3//! [`RabbitMqBackend`] implements [`queuey_core::Backend`]: it owns one
4//! AMQP connection, publishes with publisher confirms, and hands the worker
5//! runtime a stream of [`RabbitMqDelivery`] values.
6//!
7//! ```no_run
8//! use std::{sync::Arc, time::Duration};
9//!
10//! use queuey_core::{Backend, QueueConfig};
11//! use queuey_rabbitmq::{RabbitMqBackend, RabbitMqOptions};
12//!
13//! # async fn example() -> queuey_core::Result<()> {
14//! let backend = RabbitMqBackend::with_options(
15//! "amqp://guest:guest@localhost:5672/%2f",
16//! RabbitMqOptions::default().retry_granularity(Duration::from_secs(5)),
17//! )
18//! .await?;
19//!
20//! let emails = QueueConfig::new("myapp.emails").prefetch(10);
21//! backend.declare(std::slice::from_ref(&emails)).await?;
22//!
23//! let backend = Arc::new(backend);
24//! // ... hand `backend` to a `Producer` / `Worker` ...
25//! backend.close().await?;
26//! # Ok(()) }
27//! ```
28//!
29//! # Topology
30//!
31//! Each logical queue `q` is backed by two long-lived broker queues, `q` and
32//! `q.dead`, plus a short-lived *hold* queue `q.deferred.{ttl_ms}` per distinct
33//! delay. Every wait, whether a retry backoff, a delayed enqueue or a deferral,
34//! happens in a hold queue. See [`topology`] for the exact arguments.
35//!
36//! # Why hold queues, and not one wait queue with per-message expirations
37//!
38//! RabbitMQ only expires the message at the *head* of a classic queue. In a
39//! shared wait queue, a message with a five-minute `expiration` at the head
40//! holds back every one-second `expiration` queued behind it, and exponential
41//! backoff produces exactly that mix of delays. So instead the delay is part of
42//! the queue *name*, the wait is the queue-wide `x-message-ttl`, and every
43//! message in `q.deferred.30000` expires in publish order. A short wait is never
44//! stuck behind a long one, because the two live in different queues.
45//!
46//! Delays are rounded **up** to a granularity to bound how many hold queues
47//! exist at once: [`RabbitMqOptions::retry_granularity`] for retries and
48//! [`Producer::enqueue_after`](queuey_core::Producer::enqueue_after),
49//! [`RabbitMqOptions::deferred_granularity`] for deferrals, both `1s` by
50//! default. The hold queue is declared on demand right before each publish:
51//! an idle hold queue deletes itself one TTL after the last publish to it
52//! (`x-expires = 2 * TTL`), and every declare resets that timer.
53//!
54//! # Retry versus deferral
55//!
56//! Both wait in the same hold queues. They differ in what happens when the job
57//! is back on `q`:
58//!
59//! * A **retry** ([`Delivery::retry`](queuey_core::Delivery::retry), and a
60//! delayed [`Backend::publish`](queuey_core::Backend::publish)) carries
61//! priority `0` and joins the back of the queue like any other message. Its
62//! attempt counter has been incremented.
63//! * A **deferral** ([`Backend::defer`](queuey_core::Backend::defer),
64//! [`Delivery::defer`](queuey_core::Delivery::defer)) is what a
65//! `429 Too Many Requests` with `Retry-After: 30` needs: the job did not fail,
66//! must not burn an attempt, and must run **ahead of the backlog** when it
67//! returns. `q` is declared with `x-max-priority` from
68//! [`QueueConfig::max_priority`](queuey_core::QueueConfig::max_priority)
69//! (default `Some(10)`), every publish carries the envelope's `priority`, and a
70//! deferred envelope carries the queue's top level, so it is served before
71//! everything that piled up meanwhile. "Ahead of the backlog" means ahead of
72//! what is still *on* the queue: a consumer with prefetch `N` already holds up
73//! to `N` backlog messages, and the returning deferral is first among what is
74//! left.
75//!
76//! ## What a hold requires
77//!
78//! * **The queue must have been declared through this backend, in this
79//! process.** Otherwise the hold queue's durability and the queue it
80//! dead-letters back to would be guesses, and a TTL expiry into a queue that
81//! does not exist is discarded silently by the broker. Unlike a
82//! `mandatory` publish, nothing comes back and nothing is logged. Holding
83//! onto an unknown queue is
84//! [`Error::UnknownQueue`](queuey_core::Error::UnknownQueue)
85//! instead. `Producer::new` and `WorkerBuilder::build` declare the queue set;
86//! `Producer::new_undeclared` deliberately does not, so a producer built that
87//! way can enqueue, but not enqueue with a delay or defer.
88//! * **The delay must fit.** It is capped at
89//! [`MAX_DEFERRAL_MS`](topology::MAX_DEFERRAL_MS), about 24.8 days. That is half of
90//! what a 32-bit millisecond TTL can express, because the hold queue's
91//! `x-expires` is twice its TTL. A longer delay is refused rather than
92//! clamped: releasing a job early is the one thing a hold promises not to
93//! do. Rounding up to the granularity happens first, so a delay just under the
94//! cap can be refused too.
95//!
96//! Both failures happen *before* anything is acked, so from
97//! [`Delivery::retry`](queuey_core::Delivery::retry) and
98//! [`Delivery::defer`](queuey_core::Delivery::defer) they leave the
99//! original message unacknowledged and the broker redelivers it.
100//!
101//! ## Upgrading from the `q.retry` wait queue
102//!
103//! Earlier versions declared a `q.retry` queue per work queue and published
104//! retries into it with a per-message `expiration`. This version neither
105//! declares nor uses it. Nothing needs migrating: messages still waiting in an
106//! existing `q.retry` expire back onto `q` on their own, because the
107//! dead-letter routing is an argument of that queue, and workers running the
108//! old version keep declaring it themselves. Delete `q.retry` once it is empty
109//! and no old worker is left. `RabbitMqOptions::retry_suffix` is gone with it;
110//! [`RabbitMqOptions::retry_granularity`] is the retry tunable now.
111//!
112//! ## Breaking topology change
113//!
114//! `x-max-priority` is a *declaration* argument, and RabbitMQ refuses to change
115//! the arguments of a queue that already exists: the declaration is answered
116//! with `PRECONDITION_FAILED`, which closes the channel and surfaces here as an
117//! error from [`declare`](queuey_core::Backend::declare).
118//!
119//! A `q` created before this feature has no `x-max-priority`, so **declaring it
120//! again with the default config will fail**. Either:
121//!
122//! * drain and delete `q`, then let this backend redeclare it. Deferred jobs
123//! then come back ahead of the backlog; or
124//! * set `max_priority = 0` on the queue's
125//! [`QueueConfig`](queuey_core::QueueConfig) (or
126//! `#[queue(max_priority = 0)]`), which declares `q` exactly as before.
127//! Deferral still works, it just returns jobs FIFO instead of ahead of the
128//! queue.
129//!
130//! `q.dead` and hold queues are unchanged, so only `q` is affected.
131//!
132//! # Guarantees
133//!
134//! * Every publish (enqueue, retry, defer, dead-letter) is `mandatory` and
135//! confirmed by the broker before it is reported as successful. A routing key
136//! that matches no queue is returned by the broker and reported as an error
137//! rather than passing for a confirmed publish.
138//! * [`Delivery::retry`](queuey_core::Delivery::retry),
139//! [`Delivery::defer`](queuey_core::Delivery::defer) and
140//! [`Delivery::dead_letter`](queuey_core::Delivery::dead_letter)
141//! publish first and ack second, and skip the ack entirely when the publish
142//! fails, so a job is never lost. At worst it is redelivered. Different
143//! delays never block each other: each waits in its own hold queue. With
144//! [`RabbitMqOptions::declare_dead_letter_queues`] off, `dead_letter` rejects
145//! the delivery instead of publishing to a `q.dead` this backend does not own.
146//! * Messages whose body is not a valid [`queuey_core::Envelope`] are
147//! moved aside and logged, never surfaced as a stream error, so one poison
148//! message cannot stall a consumer.
149//!
150//! # Not in scope for v1
151//!
152//! Reconnection. When the connection drops, consumer streams end and further
153//! calls fail; supervising and rebuilding the backend is the caller's job.
154//!
155//! [`queuey`]: queuey_core
156
157#![forbid(unsafe_code)]
158#![warn(missing_docs)]
159
160mod backend;
161mod delivery;
162mod error;
163mod options;
164mod publisher;
165
166pub mod codec;
167pub mod topology;
168
169pub use backend::RabbitMqBackend;
170pub use delivery::RabbitMqDelivery;
171pub use options::RabbitMqOptions;
172
173/// Re-export of the `lapin` version this backend is built against, so callers
174/// can name [`lapin::ConnectionProperties`] without pinning it themselves.
175pub use lapin;