broker
ModuleBrokers: where messages are stored and how they are delivered.
Contents
4 submodules, and what the module defines itself.
use utopia_queue::broker::Provisioning;pub enum ProvisioningWhether a broker may create and rewrite a queue's server-side
configuration (Broker\Provisioning).
broker::background
Own pageBackground dispatch on top of a synchronous publisher.
use utopia_queue::broker::background::Background;pub struct Background<P>The buffer's capacity is the back pressure: once full, enqueue waits
for a slot up to timeout seconds (negative: forever) and then fails with
Error::BufferFull. With a batch interval and size, consecutive
messages for one queue are coalesced into publish_many calls, a partial
batch leaving when its oldest message reaches the interval. Before
Background::start (and after Background::shutdown) enqueue
publishes synchronously. More than one reader gives up FIFO order.
use std::sync::Arc;
use utopia_queue::broker::{Background, Redis};
use utopia_queue::connection::Redis as Connection;
use utopia_queue::{AsyncPublisher, Queue};
let redis = Arc::new(Redis::new(Arc::new(Connection::new("localhost", 6379)), Arc::new(Connection::new("localhost", 6379))));
let background = Background::new(redis, 512, 1, -1.0, None)?;
background.start()?;
background.enqueue(&Queue::new("audits")?, utopia_queue::payload(&serde_json::json!({"event": "login"}))?).await?;
background.shutdown().await;pub type Log = Arc<dyn Fn(&str) + Send + Sync>Where dispatch failures are written (stderr by default): there is no producer left to report them to.
broker::nats
Own pageThe NATS JetStream broker.
use utopia_queue::broker::nats::Nats;pub struct NatsThe NATS JetStream broker (Broker\Nats): each queue is a work-queue
stream Q_<NAME> with one subject q.<name>.normal served by the durable
pull consumer worker, and a dead stream Q_<NAME>_DEAD on q.<name>.dead.
Redelivery and dead-lettering are the server's: a rejected message is
NAK'd with the tier backoff and redelivered until max_deliver, then
terminated and copied to the dead stream; a terminal message goes there at
once. A worker that dies is reclaimed by ack_wait redelivery, so
Nats::reap does nothing. Every publish carries its pid as
Nats-Msg-Id, so a retried publish inside the duplicate window is stored
once, and the codec's Content-Type header.
use utopia_queue::broker::{Nats, NatsConfig};
use utopia_queue::{Consumer, Publisher, Queue};
let broker = Nats::new("nats://localhost:4222", NatsConfig::default())?;
let queue = Queue::new("audits")?;
broker.publish(&queue, utopia_queue::payload(&serde_json::json!({"event": "login"}))?).await?;
for message in broker.receive(&queue, 1, 10).await? {
broker.commit(&queue, &message).await?;
}use utopia_queue::broker::nats::NatsConfig;pub struct NatsConfigHow the NATS broker provisions and drives a queue's streams and consumers.
Default.pub type MessageId = Arc<dyn Fn(&Array) -> String + Send + Sync>Derives a message's deduplication id from its payload.
pub type OnError = Arc<dyn Fn(&Error) + Send + Sync>Where the broker reports failures it cannot raise.
broker::pool
Own pageBrokers leased from pools.
use utopia_queue::broker::pool::Pool;pub struct Pool<B>Undocumented.
use std::sync::Arc;
use utopia_queue::broker::{Pool, PoolResources, Redis};
use utopia_queue::connection::Redis as Connection;
let brokers = Arc::new(PoolResources::new(4, || {
Redis::new(Arc::new(Connection::new("localhost", 6379)), Arc::new(Connection::new("localhost", 6379)))
}));
let pool = Pool::new(Some(brokers.clone()), Some(brokers));use utopia_queue::broker::pool::Resources;pub struct Resources<B>Undocumented.
broker::redis
Own pageThe Redis broker.
use utopia_queue::broker::redis::Redis;pub struct RedisThe Redis broker: publishes onto a list and delivers with at-least-once semantics through Lua scripts that keep every claim recoverable.
Per queue <namespace>.<kind>.<name> keys hold the work list (queue),
in-flight pids (processing), rejected pids (failed), parked pids
(dead), undecodable bytes (poison), each claim's payload (jobs.<pid>),
heartbeat (claims.<pid>) and owner (owners.<pid>), and counters
(stats.<name>.total|processing|success|failed).
It takes two connections: receive for the blocking receive loop and its
claim writes (one caller), and commands for settlements and publishing
(wrap it in crate::connection::Locking when tasks share it).
use std::sync::Arc;
use utopia_queue::broker::Redis;
use utopia_queue::connection::Redis as Connection;
use utopia_queue::{Consumer, Publisher, Queue};
let broker = Redis::new(Arc::new(Connection::new("localhost", 6379)), Arc::new(Connection::new("localhost", 6379)));
let queue = Queue::new("emails")?;
broker.publish(&queue, utopia_queue::payload(&serde_json::json!({"to": "ada@example.com"}))?).await?;
for message in broker.receive(&queue, 1, 10).await? {
broker.commit(&queue, &message).await?;
}pub type ReconnectCallback = Box<dyn Fn(&Queue, &Error, i64, i64) + Send + Sync>Called when a receive fails on the transport: the queue, the error, the attempt number and the milliseconds the broker sleeps before returning.
pub type ReconnectSuccessCallback = Box<dyn Fn(&Queue, i64) + Send + Sync>Called on the first successful receive after failures, with their count.