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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! The classic queue descriptor.
use ;
use NonZeroU16;
use Duration;
use ;
use Bindings;
use IntoSource;
use ;
use ;
use crateConnectedLapinBroker;
use crateDelay;
use crateAmqpError;
use crateRabbitExchange;
use crateLapinSubscriber;
/// Describes one classic-queue subscription: the queue, its expected settings, and its bindings.
///
/// The classic queue is the single-node implementation, and it counts nothing: a delivery says
/// only that it has been seen before, never how often. So a registration's `max_attempts(n)` is
/// the runtime's to apply, counted with the framework's own header on the copies it publishes,
/// and `out_retry(policy)` names the publisher those copies leave through. A queue that should
/// end a poison loop on the server instead is a [`RabbitQuorumQueue`](crate::RabbitQuorumQueue).
///
/// Descriptors describe the EXPECTED topology for routing; by default nothing is created on the
/// broker (managing infrastructure is the user's job). Opt in to declaration per broker with
/// [`declare_topology(true)`](crate::LapinBroker::declare_topology).
///
/// # Examples
///
/// ```
/// use ruststream_lapin::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct OrderPlaced {
/// id: u64,
/// }
///
/// #[subscriber(RabbitQueue::new("orders")
/// .bind(RabbitExchange::topic("events"), "order.*")
/// .dead_letter_exchange("dead-letters"))]
/// async fn on_order(event: &OrderPlaced) -> HandlerOutcome {
/// if event.id == 0 {
/// // Rejected without requeue: the queue's dead-letter exchange takes it.
/// return HandlerOutcome::drop();
/// }
/// HandlerOutcome::ack()
/// }
///
/// fn app() -> RustStream {
/// let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
/// RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(broker, |b| {
/// b.include(on_order);
/// })
/// }
/// ```
///
/// A slice parameter asks for a batch, and the mount site says how big it may get:
///
/// ```
/// use std::time::Duration;
///
/// use ruststream_lapin::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Settlement {
/// id: u64,
/// }
///
/// #[subscriber(RabbitQueue::new("settlements")
/// .prefetch(nonzero!(64))
/// .batch_wait(Duration::from_millis(200)))]
/// async fn on_settlements(settlements: &[Settlement]) -> HandlerOutcome {
/// tracing::info!(count = settlements.len(), "settling");
/// HandlerOutcome::ack()
/// }
///
/// fn app() -> RustStream {
/// let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
/// RustStream::new(AppInfo::new("settlements", "0.1.0")).with_broker(broker, |b| {
/// b.include(on_settlements.batch(nonzero!(32)));
/// })
/// }
/// ```
///
/// The runtime caps the retries of a classic queue and publishes each copy through the
/// `out_retry` policy, here the confirms one:
///
/// ```
/// use ruststream_lapin::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Refund {
/// id: u64,
/// }
///
/// #[subscriber(RabbitQueue::new("refunds"))]
/// async fn on_refund(refund: &Refund) -> HandlerOutcome {
/// tracing::warn!(refund.id, "the provider is down, trying again");
/// HandlerOutcome::retry()
/// }
///
/// fn app() -> RustStream {
/// let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
/// RustStream::new(AppInfo::new("refunds", "0.1.0")).with_broker(broker, |b| {
/// b.include(on_refund)
/// .max_attempts(nonzero!(5u32))
/// .dead_letter("refunds.dead")
/// .out_retry(TransactionalPublish::default());
/// })
/// }
/// ```
queue_settings!;
/// The descriptor is its own source, so the manual path's `subscriber(source, body)` takes a
/// `RabbitQueue` where the attribute path writes it in the decorator.
/// The queue name: on the default exchange a routing key addresses the queue that carries it, so
/// that is where the runtime's retry copy reaches this subscription again.
///
/// The answer holds for a retry publisher on the default exchange, which is what
/// [`LapinPublish`](crate::LapinPublish) is unless configured otherwise. A publisher aimed at a
/// topic or direct exchange reaches the queue only through a binding under this name, so bind it
/// there or leave the retry publisher on the default exchange.