1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! Messaging for id_effect services: a broker-agnostic `MessageBroker`, an in-memory broker, and
//! the transactional outbox, inbox and relay on `id_effect_sql`.
//!
//! [`Topic`] is the validated name messages are published to: 1 to [`Topic::MAX_LEN`] ASCII
//! letters, digits, `.`, `_` or `-`, a name every supported broker accepts.
//!
//! ```
//! use id_effect_messaging::{Topic, TopicError};
//!
//! let topic = Topic::new("ledger.entry-posted").unwrap();
//! assert_eq!(topic.to_string(), "ledger.entry-posted");
//! assert_eq!(Topic::new("a b"), Err(TopicError::InvalidChar { ch: ' ', index: 1 }));
//! ```
//!
//! # Brokers
//!
//! A [`MessageBroker`] publishes [`Message`]s to a topic and hands them to **consumer groups**:
//! every group sees every message of the topic, and the consumers of one group share them. A
//! subscription is a [`Stream`](id_effect::Stream) of [`Delivery`]s; each is settled once, with
//! [`ack`](Delivery::ack) or [`nack`](Delivery::nack). A nacked (or dropped) delivery is
//! redelivered until [`SubscribeOptions::max_deliveries`], then moved to the dead-letter topic;
//! at most [`SubscribeOptions::prefetch`] deliveries of a subscription are unsettled at a time.
//! [`MemoryBroker`] implements all of it in memory, for tests and single-process development.
//!
//! ```
//! use id_effect_messaging::{
//! ConsumerGroup, MemoryBroker, Message, MessageBroker, Nack, SubscribeOptions, Topic,
//! };
//!
//! let broker = MemoryBroker::new();
//! let orders = Topic::new("orders").unwrap();
//! let billing = ConsumerGroup::new("billing").unwrap();
//! let opts = SubscribeOptions::default().max_deliveries(2);
//! let eff = broker
//! .publish(&orders, Message::new("order 1"))
//! .flat_map({
//! let (broker, orders, billing) = (broker.clone(), orders.clone(), billing.clone());
//! move |()| broker.subscribe(&orders, &billing, opts)
//! })
//! .flat_map(|deliveries| deliveries.take(2).run_for_each(|d| d.nack(Nack::Requeue)));
//! assert!(eff.block_on().into_result().is_ok());
//! // Delivered twice, nacked twice: dead-lettered.
//! let dlq = broker.messages(&Topic::new("orders.dlq").unwrap());
//! assert_eq!(dlq[0].payload(), b"order 1");
//! ```
//!
//! # Outbox, relay and inbox
//!
//! [`Outbox::stage`] writes a message to the table `id_effect_outbox` with the caller's
//! `SqlTransaction`, so it is published if and only if the caller's work commits. A [`Relay`]
//! claims the committed rows from an [`OutboxStore`] ([`PgOutbox`]: `FOR UPDATE SKIP LOCKED`
//! with a lease, so several relays share the outbox), publishes each through any
//! [`MessageBroker`] with its staged [`MessageId`], marks it sent, retries a failed publish on a
//! core `Schedule` ([`RelayConfig::retry`]) and prunes sent rows. Delivery is at least once (a
//! relay that crashes after publishing publishes again), so consumers deduplicate with
//! [`Inbox::once`]: it records `(consumer group, message id)` in `id_effect_inbox` with the
//! handler's transaction and runs the handler only the first time; [`Inbox::handler`] acks after
//! the commit. [`migrator`] ships both tables, tracked in their own table [`MIGRATIONS_TABLE`].
//! [`MemoryOutbox`] and [`MemoryInbox`] are the same in memory, for tests.
//!
//! ```
//! use std::sync::Arc;
//!
//! use id_effect_messaging::{
//! ConsumerGroup, MemoryBroker, MemoryInbox, MemoryOutbox, Message, MessageId, Once, Relay,
//! Topic,
//! };
//!
//! let (outbox, broker) = (MemoryOutbox::new(), MemoryBroker::new());
//! let orders = Topic::new("orders").unwrap();
//! let staged = outbox.stage(&orders, Message::new("o-1").with_id("o-1"));
//! assert!(staged.block_on().into_result().is_ok());
//! let relay = Relay::new(Arc::new(outbox.clone()), Arc::new(broker.clone()));
//! let report = relay.run_until_idle().block_on().into_result().unwrap();
//! assert_eq!((report.published, broker.messages(&orders).len()), (1, 1));
//!
//! let inbox = MemoryInbox::new();
//! let (billing, id) = (ConsumerGroup::new("billing").unwrap(), MessageId::new("o-1"));
//! let first = inbox.once(&billing, &id, || id_effect::succeed::<_, (), _>("billed"));
//! let again = inbox.once(&billing, &id, || id_effect::succeed::<_, (), _>("billed twice"));
//! assert_eq!(first.block_on().into_result(), Ok(Once::Ran("billed")));
//! assert_eq!(again.block_on().into_result(), Ok(Once::Duplicate));
//! ```
//!
//! Tier `platform` (ADR 0010 Amendment 6): the Ledger reference app under
//! `examples/reference/` depends on it.
pub use ;
pub use ;
pub use ;
pub use MemoryBroker;
pub use ;
pub use ;
pub use ;
use fmt;
/// A validated topic name.
;
/// The rules of a [`Topic`] (and a [`ConsumerGroup`]) name.
pub
/// Why a name is not a valid [`Topic`].