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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Streaming entry points on
//! [`MessageAssemblyState`](crate::message_assembler::MessageAssemblyState).
//!
//! These methods keep streaming partials in their own map so they never
//! contribute to the aggregate assembly budgets, and report evicted streaming
//! keys separately so the connection can fail a waiting handler's body.
use std::time::Instant;
use super::{
ContinuationFrameHeader,
MessageAssemblyError,
MessageKey,
MessageSeriesError,
StreamingContinuation,
StreamingFailure,
StreamingFirstFrame,
budget::check_size_limit,
streaming::{StreamingAssembly, StreamingAssemblyId},
validation,
};
/// Keys evicted by
/// [`MessageAssemblyState::purge_expired_detailed`](crate::message_assembler::MessageAssemblyState::purge_expired_detailed).
///
/// Streaming assemblies are reported separately so the connection can fail the
/// handler's body with a timeout instead of leaving it waiting forever. They
/// are reported by identity rather than by key, because the key may already
/// have been reused by a replacement assembly.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PurgedAssemblies {
/// Buffered assemblies that were discarded.
buffered: Vec<MessageKey>,
/// Streaming assemblies whose consumer must be notified.
streaming: Vec<StreamingAssemblyId>,
}
impl PurgedAssemblies {
/// Return the evicted buffered assembly keys.
#[must_use]
pub fn buffered(&self) -> &[MessageKey] { &self.buffered }
/// Return the evicted streaming assemblies.
#[must_use]
pub fn streaming(&self) -> &[StreamingAssemblyId] { &self.streaming }
/// Return whether no assembly was evicted.
#[must_use]
pub const fn is_empty(&self) -> bool { self.buffered.is_empty() && self.streaming.is_empty() }
}
impl super::MessageAssemblyState {
/// Admit a first frame for immediate delivery to a streaming route.
///
/// A single-frame message is returned without inserting any partial state.
/// A multi-frame message inserts a streaming partial that retains only its
/// ordering and byte-count state, so it contributes nothing to
/// [`total_buffered_bytes`](Self::total_buffered_bytes).
///
/// # Errors
///
/// Returns [`MessageAssemblyError::DuplicateFirstFrame`] when the key is
/// already assembling, [`MessageAssemblyError::MessageTooLarge`] when the
/// declared message cannot fit the per-message cap, or a declared-total
/// error when the first frame contradicts its declaration.
pub fn accept_streaming_first_frame<'a>(
&mut self,
input: super::FirstFrameInput<'a>,
) -> Result<StreamingFirstFrame<'a>, MessageAssemblyError> {
self.accept_streaming_first_frame_at(input, Instant::now())
}
/// Admit a streaming first frame using an explicit clock reading.
///
/// # Errors
///
/// See [`accept_streaming_first_frame`](Self::accept_streaming_first_frame).
pub fn accept_streaming_first_frame_at<'a>(
&mut self,
input: super::FirstFrameInput<'a>,
now: Instant,
) -> Result<StreamingFirstFrame<'a>, MessageAssemblyError> {
self.purge_deferring_streaming(now);
let key = input.header.message_key;
if self.assemblies.contains_key(&key) || self.streaming_assemblies.contains_key(&key) {
return Err(MessageAssemblyError::DuplicateFirstFrame { key });
}
validation::validate_first_frame(input.header, input.body.len())?;
// The cap counts what the message retains — metadata plus body — and a
// declared total may only tighten it. Counting the larger of the two
// stops an undersized first frame from understating the message, and
// the checked helper rejects an unrepresentable sum instead of
// saturating it to a value a `usize::MAX` limit would accept.
let counted_body_len = input
.header
.total_body_len
.unwrap_or(0)
.max(input.body.len());
check_size_limit(
self.max_message_size,
key,
input.metadata.len(),
counted_body_len,
)?;
let chunk = input.body;
let is_last = input.header.is_last;
let routing = input.routing;
// A single-frame message tracks no partial, so it has no identity to
// evict or discard later.
let mut assembly = None;
if !is_last {
self.streaming_generation = self.streaming_generation.wrapping_add(1);
let id = StreamingAssemblyId::new(key, self.streaming_generation);
let partial = StreamingAssembly::from_first_frame(id, &input, chunk.len(), now);
self.streaming_assemblies.insert(key, partial);
assembly = Some(id);
}
Ok(StreamingFirstFrame {
key,
assembly,
routing,
metadata: input.metadata,
chunk,
is_last,
})
}
/// Admit a continuation frame for an existing streaming message.
///
/// # Errors
///
/// Returns a [`StreamingFailure`] when continuity, size, or declared-total
/// validation fails. `is_terminal` reports whether the partial was removed
/// and the handler's stream must be closed.
pub fn accept_streaming_continuation<'a>(
&mut self,
header: &ContinuationFrameHeader,
body: &'a [u8],
) -> Result<StreamingContinuation<'a>, StreamingFailure> {
self.accept_streaming_continuation_at(header, body, Instant::now())
}
/// Admit a streaming continuation using an explicit clock reading.
///
/// # Errors
///
/// See [`accept_streaming_continuation`](Self::accept_streaming_continuation).
pub fn accept_streaming_continuation_at<'a>(
&mut self,
header: &ContinuationFrameHeader,
body: &'a [u8],
now: Instant,
) -> Result<StreamingContinuation<'a>, StreamingFailure> {
self.purge_deferring_streaming(now);
let key = header.message_key;
if header.body_len != body.len() {
return Err(StreamingFailure::new(
key,
MessageAssemblyError::Series(MessageSeriesError::ContinuationBodyLengthMismatch {
key,
header_len: header.body_len,
actual_len: body.len(),
}),
false,
));
}
let Some(partial) = self.streaming_assemblies.get_mut(&key) else {
return Err(StreamingFailure::new(
key,
MessageAssemblyError::Series(MessageSeriesError::MissingFirstFrame { key }),
true,
));
};
let outcome = partial.accept_continuation(header, body, self.max_message_size);
match outcome {
Ok(Some(chunk)) => {
let is_last = chunk.is_last;
if is_last {
self.streaming_assemblies.remove(&key);
}
Ok(StreamingContinuation::Chunk {
key,
chunk: chunk.body,
is_last,
})
}
Ok(None) => {
let complete = partial.is_complete();
if complete {
self.streaming_assemblies.remove(&key);
}
Ok(StreamingContinuation::Discarded { key })
}
Err(failure) => {
if failure.is_terminal() {
self.streaming_assemblies.remove(&key);
}
Err(failure)
}
}
}
/// Return whether `key` is currently assembling for a streaming route.
#[must_use]
pub fn is_streaming_key(&self, key: MessageKey) -> bool {
self.streaming_assemblies.contains_key(&key)
}
/// Switch `key` into discard mode so remaining frames are validated but
/// not delivered.
///
/// Use this only when the caller's frame was validated against the
/// assembly currently registered for `key`. A notification raised earlier
/// must use [`Self::discard_streaming_if`] instead, because the key may
/// have been reused by a replacement assembly in the meantime.
///
/// Returns `false` when no streaming assembly exists for the key.
pub fn discard_streaming(&mut self, key: MessageKey) -> bool {
self.streaming_assemblies
.get_mut(&key)
.is_some_and(|partial| {
partial.begin_discard();
true
})
}
/// Switch one specific assembly into discard mode.
///
/// Returns `false` when the assembly is gone or when the key now names a
/// replacement, leaving that replacement untouched.
pub fn discard_streaming_if(&mut self, id: StreamingAssemblyId) -> bool {
self.streaming_assemblies
.get_mut(&id.key())
.is_some_and(|partial| {
if partial.id() != id {
return false;
}
partial.begin_discard();
true
})
}
/// Number of streaming assemblies currently tracked.
#[must_use]
pub fn streaming_count(&self) -> usize { self.streaming_assemblies.len() }
/// Purge expired state during admission, deferring streaming evictions.
///
/// An evicted buffered assembly needs no notification, but an evicted
/// streaming assembly leaves a handler waiting on a body that will never
/// complete. Only the connection loop can fail that body, and it learns
/// about evictions from [`Self::purge_expired_detailed`], so evictions made
/// here are queued for the next call rather than dropped.
pub(super) fn purge_deferring_streaming(&mut self, now: Instant) {
let purged = self.purge_expired_detailed_at(now);
self.deferred_streaming_evictions
.extend_from_slice(purged.streaming());
}
/// Remove expired buffered and streaming assemblies.
#[must_use]
pub fn purge_expired_detailed(&mut self) -> PurgedAssemblies {
self.purge_expired_detailed_at(Instant::now())
}
/// Remove expired assemblies using an explicit clock reading.
#[must_use]
pub fn purge_expired_detailed_at(&mut self, now: Instant) -> PurgedAssemblies {
let timeout = self.timeout;
let mut purged = PurgedAssemblies::default();
purged
.streaming
.append(&mut self.deferred_streaming_evictions);
self.assemblies.retain(|key, partial| {
let expired = now.saturating_duration_since(partial.started_at) >= timeout;
if expired {
purged.buffered.push(*key);
}
!expired
});
self.streaming_assemblies.retain(|_key, partial| {
let expired = now.saturating_duration_since(partial.started_at) >= timeout;
if expired {
purged.streaming.push(partial.id());
}
!expired
});
purged
}
}