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
227
228
229
230
231
232
233
234
235
236
//! The partition key: a per-message option on this crate's publishers, set by a step on the
//! publish builder.
//!
//! Redis has no partition of its own, so the resolved key travels as the well-known
//! [`PARTITION_KEY_HEADER`] header - the wire form the consumer side's
//! [`Partitioned`](ruststream::Partitioned) reads back, on all three transports.
use HeaderMap;
use ;
use cratePARTITION_KEY_HEADER;
/// The per-message settings of every publisher and transaction in this crate.
///
/// One field, the partition key, and it is optional like every field of an options type: a publish
/// that names no step carries no options at all, and nothing is written into its headers.
///
/// The type is the bound that keeps this crate's builder steps off another broker's publisher, and
/// it is what a handler body names when it adjusts a setting
/// (`Out<impl Publisher<Options = RedisPublishOptions>, Marker>`). A test reads the value back off
/// the slot view with `with_options`.
///
/// # Examples
///
/// ```
/// # use std::error::Error;
/// # #[cfg(feature = "testing")]
/// # mod demo {
/// use std::error::Error;
///
/// use ruststream::testing::TestApp;
/// use ruststream_fred::stream::prelude::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize, Serialize, Outgoing)]
/// struct Order {
/// tenant: String,
/// id: u64,
/// }
///
/// #[derive(OutSlot)]
/// #[publishes(Order)]
/// struct Ledger;
///
/// #[subscriber(RedisStream::new("orders.in").group("workers"))]
/// async fn forward(
/// order: &Order,
/// Out(ledger): Out<impl Publisher<Options = RedisPublishOptions>, Ledger>,
/// ) -> HandlerOutcome {
/// let sent = ledger
/// .message(order)
/// .to("orders.keyed")
/// .partition_key(&order.tenant)
/// .publish()
/// .await;
/// match sent {
/// Ok(_) => HandlerOutcome::ack(),
/// Err(_) => HandlerOutcome::retry(),
/// }
/// }
///
/// pub fn app() -> impl App<State = ()> {
/// RustStream::new(AppInfo::new("orders", "0.1.0"))
/// .with_broker(RedisBroker::standalone("redis://localhost:6379"), |b| {
/// b.include(forward).out(Ledger, Publish).build();
/// })
/// }
///
/// pub async fn the_ledger_is_keyed_by_tenant() -> Result<(), Box<dyn Error>> {
/// let tb = TestApp::start(app()).await?;
/// let order = Order {
/// tenant: "tenant-a".to_owned(),
/// id: 7,
/// };
/// tb.broker::<RedisBroker>()
/// .message(&order)
/// .to("orders.in")
/// .publish()
/// .await?;
///
/// tb.out::<Ledger>()
/// .assert_called_once()
/// .with_options(&RedisPublishOptions {
/// partition_key: Some(b"tenant-a".to_vec()),
/// ..RedisPublishOptions::default()
/// });
/// Ok(())
/// }
/// # }
/// # #[cfg(feature = "testing")]
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # tokio::runtime::Runtime::new()?.block_on(demo::the_ledger_is_keyed_by_tenant())
/// # }
/// # #[cfg(not(feature = "testing"))]
/// # fn main() {}
/// ```
/// The steps this crate adds to the publish builder.
///
/// Import it from any of this crate's preludes to reach [`partition_key`](Self::partition_key) on
/// a publish over a Redis publisher. The impl is bounded on [`RedisPublishOptions`], so the step
/// does not appear on a builder over another broker's publisher.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::stream::prelude::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize, Serialize, Outgoing)]
/// #[outgoing(name = "orders")]
/// struct Order {
/// tenant: String,
/// id: u64,
/// }
///
/// #[derive(OutSlot)]
/// #[publishes(Order)]
/// struct Orders;
///
/// // Every order of one tenant lands in the same `workers(n, by_key)` lane downstream, so their
/// // relative order is preserved while other tenants run in parallel.
/// #[subscriber(RedisStream::new("orders.in").group("intake"))]
/// async fn intake(
/// order: &Order,
/// Out(orders): Out<impl Publisher<Options = RedisPublishOptions>, Orders>,
/// ) -> HandlerOutcome {
/// match orders.message(order).partition_key(&order.tenant).publish().await {
/// Ok(_) => HandlerOutcome::ack(),
/// Err(_) => HandlerOutcome::retry(),
/// }
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
/// RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
/// RedisBroker::standalone("redis://localhost:6379"),
/// |b| {
/// b.include(intake).out(Orders, Publish).build();
/// },
/// )
/// }
/// # }
/// # fn main() {}
/// ```
/// The headers a publish leaves with, once `options` are resolved over the ones it carries.
///
/// A step wins over the header the call site wrote itself, because it names this one message while
/// the header map may be a contract the message type declares. A publish no step touched keeps its
/// headers untouched, so writing [`PARTITION_KEY_HEADER`] by hand stays the portable spelling.
/// The map arrives owned because a publish owns it: it is what the transforms filled, taken out
/// of the outgoing message, so resolving a step writes into it instead of copying it.
pub