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
//! Multi-packet channel state and correlation stamping.
use std::fmt;
use tokio::sync::mpsc;
/// Multi-packet correlation stamping state.
///
/// Tracks the active receiver and how frames should be stamped before emission.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MultiPacketStamp {
/// Stamping is disabled because no multi-packet channel is active.
Disabled,
/// Stamping is enabled and frames are stamped with the provided identifier.
Enabled(Option<u64>),
}
/// Reasons why a multi-packet stream closed.
///
/// The reason informs logging severity so operators can distinguish between
/// natural completion and abrupt termination.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum MultiPacketTerminationReason {
/// The sender dropped the channel after producing all frames.
Drained,
/// The sender was dropped without yielding an explicit end-of-stream.
Disconnected,
/// Shutdown cancelled the stream before it completed.
Shutdown,
}
impl MultiPacketTerminationReason {
/// Provide a stable low-cardinality label for logs and metrics.
pub(super) const fn as_str(self) -> &'static str {
match self {
Self::Drained => "drained",
Self::Disconnected => "disconnected",
Self::Shutdown => "shutdown",
}
}
}
impl fmt::Display for MultiPacketTerminationReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) }
}
/// Multi-packet channel state tracking the active receiver and stamping config.
pub(super) struct MultiPacketContext<F> {
/// Receiver owned by the actor only while this output is active. Calling
/// `set_multi_packet_with_correlation(None, ...)` may clear or replace it
/// before channel closure or shutdown.
channel: Option<mpsc::Receiver<F>>,
/// Correlation metadata applied to frames emitted from the receiver.
stamp: MultiPacketStamp,
}
impl<F> MultiPacketContext<F> {
/// Start with no receiver so stamping cannot occur accidentally.
pub(super) const fn new() -> Self {
Self {
channel: None,
stamp: MultiPacketStamp::Disabled,
}
}
/// Install a multi-packet channel with an optional correlation identifier.
///
/// When `channel` is `Some`, stamping is enabled with the provided `correlation_id`.
/// When `channel` is `None`, stamping is disabled and `correlation_id` is ignored.
pub(super) fn install(
&mut self,
channel: Option<mpsc::Receiver<F>>,
correlation_id: Option<u64>,
) {
let stamp = if channel.is_some() {
MultiPacketStamp::Enabled(correlation_id)
} else {
MultiPacketStamp::Disabled
};
self.channel = channel;
self.stamp = stamp;
}
/// Borrow the receiver without transferring actor ownership.
pub(super) const fn channel_mut(&mut self) -> Option<&mut mpsc::Receiver<F>> {
self.channel.as_mut()
}
/// Returns `true` if correlation stamping is enabled.
pub(super) const fn is_stamping_enabled(&self) -> bool {
matches!(self.stamp, MultiPacketStamp::Enabled(_))
}
/// Return the identifier to stamp on frames, if stamping is enabled.
pub(super) const fn correlation_id(&self) -> Option<u64> {
match self.stamp {
MultiPacketStamp::Enabled(value) => value,
MultiPacketStamp::Disabled => None,
}
}
}
#[cfg(test)]
mod tests {
//! Unit tests for the stamping invariant: stamping is enabled exactly
//! when a channel is installed.
use tokio::sync::mpsc;
use super::MultiPacketContext;
#[test]
fn stamping_tracks_channel_installation() {
let mut ctx = MultiPacketContext::<u8>::new();
assert!(
!ctx.is_stamping_enabled(),
"a fresh context must not stamp frames"
);
let (_tx, rx) = mpsc::channel(1);
ctx.install(Some(rx), Some(7));
assert!(
ctx.is_stamping_enabled(),
"installing a channel must enable stamping"
);
assert_eq!(
ctx.correlation_id(),
Some(7),
"ctx.correlation_id() should equal Some(7)"
);
ctx.install(None, Some(9));
assert!(
!ctx.is_stamping_enabled(),
"removing the channel must disable stamping"
);
assert_eq!(
ctx.correlation_id(),
None,
"a disabled context must not expose a correlation id"
);
}
}