id_effect_messaging
Messaging for id_effect services: the broker-agnostic MessageBroker (publish, subscribe, ack), an
in-memory broker, and the transactional outbox, inbox and relay on id_effect_sql. Redis Streams
implement the broker in id_effect_redis (feature streams) (ADR 0010 Amendment 6).
Topic is the validated name messages are published to: 1 to 249 ASCII letters, digits, ., _
or -, a name every supported broker accepts.
use ;
let topic = new.unwrap;
assert_eq!;
assert_eq!;
MessageBroker is one trait for every broker: publish(topic, Message), and subscribe(topic, group, SubscribeOptions { prefetch, concurrency, max_deliveries, dead_letter, start }) returning a
Stream of Deliverys, each settled once with ack() or nack(Nack::Requeue | Nack::DeadLetter).
Every consumer group gets every message; the subscriptions of a group share them. A requeued (or
dropped) delivery comes again until max_deliveries, then moves to the dead-letter topic
(<topic>.dlq by default) with x-dead-letter-* headers; at most prefetch deliveries of a
subscription are unsettled at once, and consume runs concurrency handlers at a time.
MemoryBroker implements all of it in memory for tests; Message::encode / decode carry
Schema-typed payloads as JSON.
use ;
let broker = new;
let orders = new.unwrap;
let billing = new.unwrap;
assert!;
let acked = broker
.subscribe
.flat_map
.block_on;
assert!;
assert_eq!;
The transactional outbox, relay and inbox run on id_effect_sql. Outbox::stage(&tx, topic, message) writes the message to id_effect_outbox with the caller's SqlTransaction, so it is
published if and only if the caller's work commits. A Relay claims committed rows from
PgOutbox with FOR UPDATE SKIP LOCKED and a lease (several relays share the outbox; a crashed
relay's rows are claimed again when its lease expires), publishes each through any
MessageBroker with its staged MessageId, marks it sent, retries a failed publish on a core
Schedule and prunes sent rows. Delivery is at least once, so Inbox::once(&tx, group, message_id, f) records (group, message id) in id_effect_inbox in the handler's transaction
and runs f only the first time; Inbox::handler wraps it into a consume handler that acks
after the commit. migrator() ships both tables (tracked in _id_effect_messaging_migrations);
MemoryOutbox and MemoryInbox are the same in memory for tests.
Tier platform: the Ledger reference app under examples/reference/ depends on it.