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
//! Typed subscription middleware around the handler.
use crate;
use ;
/// The outcome of [`Middleware::pre_handler`].
/// Middleware that runs before and after the handler.
///
/// Both hooks are asynchronous, so a middleware can await external lookups
/// such as a schema registry, a cache, or a metadata service. Implement them
/// with `async fn`; the returned futures must be `Send`. The runtime awaits
/// each hook before starting the next one, so hooks of one delivery never run
/// concurrently. A hook without `.await` points runs to completion in its
/// first poll.
///
/// `pre_handler` runs once per delivery, in registration order, before the
/// handler. It receives the input produced by the previous middleware and
/// either continues with a possibly transformed input or intercepts the
/// delivery. Handler retries reuse the transformed input without rerunning
/// `pre_handler`.
///
/// `post_handler` runs in registration order once per output, including
/// outputs from an intercepting middleware, after the handler succeeds and
/// before the sink prepares any output. It receives the input as decoded from
/// the source, before any `pre_handler` transformation, so metadata can be
/// inherited from the received record. Publish retries reuse the prepared
/// output, so a middleware never runs twice for the same value.
///
/// Input and output types are checked at compile time: a middleware written
/// for one broker's record and publish types cannot be registered on a
/// subscription with a different source or sink. Both hooks default to doing
/// nothing.
///
/// Error classification for both hooks: `Reject` routes the input as decoded
/// from the source to the subscription's dead-letter sink and then
/// acknowledges it, like a handler rejection. `Retry` and `Fatal` stop
/// processing without ACK; a middleware error never reruns the handler or the
/// middleware.
/// Middleware backed by a function or closure `Fn(&Input, Output) -> Result<Output>`
/// used as its `post_handler`.
///
/// Use it for synchronous output mappings that the built-in adapter middleware
/// does not cover, including cross-platform conversions between broker record
/// and publish types. Implement [`Middleware`] directly when the mapping needs
/// to await.