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
//! The live streaming exchange on one connection.
//!
//! A connection admits one request body at a time. The exchange that owns it
//! keeps the handler future, the body sender, and at most one in-flight chunk
//! admission together, so the loop never has to reconstruct a partially
//! consumed exchange, and dropping it cancels the handler and releases queued
//! bytes in one step.
//!
//! Only the exchange lives here. The slot that owns it, and the account every
//! chunk is charged to, are in [`stream_slot`](super::stream_slot).
use std::{future::Future, pin::Pin, time::Duration};
use bytes::Bytes;
use tokio::time::timeout;
use super::{stream_diagnostics, stream_handles::ChunkBudget};
use crate::{
message_assembler::{MessageKey, StreamingAssemblyId},
metrics::StreamingBodyOutcome,
request::{AccountedBodySender, SendChunkError},
};
/// Future returned by a streaming route handler.
pub(super) type HandlerFuture = Pin<Box<dyn Future<Output = Option<Bytes>> + Send + 'static>>;
/// One step while a chunk admission is in flight.
pub(super) enum PendingEvent {
/// The chunk was queued, or the consumer had gone away.
Send(Result<(), SendChunkError>),
/// The handler resolved first; the admission is cancelled.
Handler(Option<Bytes>),
/// The bounded wait elapsed with the admission still queued.
Idle,
}
/// Identity of the request whose body is streaming.
///
/// Carried into the terminal paths so a diagnostic can name the message
/// without the helper reaching back into the exchange it is tearing down.
#[derive(Clone, Copy, Debug)]
pub(super) struct BodyIdentity {
/// Route identifier of the message.
pub(super) message_id: u32,
/// Correlation identifier inherited from the first frame, when present.
pub(super) correlation_id: Option<u64>,
}
/// The one live streaming exchange on a connection.
pub(super) struct ActiveStream {
/// Assembly key used to match continuation frames.
pub(super) key: Option<MessageKey>,
/// Identity of the partial this exchange owns, absent for a single-frame
/// message or when no assembler participates.
pub(super) assembly: Option<StreamingAssemblyId>,
/// Route identifier used to address the reply.
pub(super) route_id: u32,
/// Correlation identifier inherited from the first frame.
pub(super) correlation_id: Option<u64>,
/// Body sender; `None` once the body is complete or abandoned.
pub(super) sender: Option<AccountedBodySender>,
/// Handler awaiting the body.
pub(super) handler: HandlerFuture,
/// Chunk admission in flight.
pub(super) pending: Option<PendingSend>,
}
/// One chunk waiting for byte admission and channel capacity.
///
/// The chunk is kept rather than a future that admitted it, because admission
/// is re-decided on every drive: the bytes retained outside the queue can only
/// shrink while a chunk waits, and a chunk refused against a stale, larger
/// total would be affordable moments later. Re-reserving is safe, because a
/// cancelled attempt releases whatever it had reserved.
pub(super) struct PendingSend {
/// Sender the chunk is admitted to.
pub(super) sender: AccountedBodySender,
/// The chunk still waiting for admission.
pub(super) chunk: Bytes,
/// Whether this chunk also completes the message.
pub(super) is_last: bool,
}
impl PendingSend {
/// Return whether this chunk also completes the message.
///
/// A caller that has to decide the body's fate without admitting the chunk
/// needs this: a chunk that ends the body leaves it whole, while any other
/// chunk leaves it truncated.
pub(super) const fn is_last(&self) -> bool { self.is_last }
}
impl ActiveStream {
/// Return the identity of the message this exchange is serving.
pub(super) const fn identity(&self) -> BodyIdentity {
BodyIdentity {
message_id: self.route_id,
correlation_id: self.correlation_id,
}
}
/// Drive the in-flight chunk admission alongside the handler.
///
/// The wait is bounded by `wait` so the caller regains control even when
/// neither side makes progress. An inbound frame that arrives while the
/// chunk is queued is therefore not dispatched from here: a continuation
/// for the same message would be mistaken for an abandoned body. The
/// caller keeps such a frame for the step after admission, and the timeout
/// is what keeps assembly expiry and the memory budget observable while
/// the chunk waits.
pub(super) async fn drive_pending(
&mut self,
wait: Duration,
budget: ChunkBudget,
) -> PendingEvent {
let Self {
pending, handler, ..
} = self;
let Some(send) = pending else {
// The caller only drives this branch when a send is pending.
return PendingEvent::Send(Ok(()));
};
// Rebuilt from the current budget on every drive. The measurement taken
// when the chunk was queued can only overstate what is retained
// elsewhere, and overstating it refuses a chunk that has since become
// affordable; the queued chunk itself is preserved across attempts.
let admission =
send.sender
.clone()
.send_chunk(send.chunk.clone(), budget.other_retained, budget.limit);
let exchange = async {
tokio::select! {
result = admission => PendingEvent::Send(result),
outcome = handler.as_mut() => PendingEvent::Handler(outcome),
}
};
match timeout(wait, exchange).await {
Ok(event) => event,
Err(_elapsed) => {
// The chunk is still unadmitted, so the connection reports a
// body that cannot make progress rather than one that ended.
StreamingBodyOutcome::AdmissionTimeout.record();
stream_diagnostics::admission_timed_out(self.identity(), wait);
PendingEvent::Idle
}
}
}
/// Poll the handler until it produces a reply.
///
/// The connection loop selects this future against the next inbound read,
/// so a handler that finishes while its body is still arriving is not
/// delayed behind a peer that sends nothing.
pub(super) async fn poll_handler(&mut self) -> Option<Bytes> { (&mut self.handler).await }
/// Take both open halves and count the exchange abandoned if either lived.
///
/// A chunk still waiting for admission, or an open body whose next chunk
/// never arrived because the handler resolved first, both mean the handler
/// will never read the rest of its body. A body with neither is already
/// complete, and counting it here would report a delivered body as
/// abandoned. Taking the halves is also what releases them, so the caller
/// holds no byte account while it writes the reply.
pub(super) fn record_unfinished(&mut self) {
let pending = self.pending.take();
let sender = self.sender.take();
if pending.is_some() || sender.is_some() {
StreamingBodyOutcome::Abandoned.record();
}
}
}