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
//! Push-queue delivery for the connection actor.
//!
//! The actor drives two frame shapes. Response frames are application
//! [`Envelope`] values that the actor fragments and hooks before a transport
//! sink serializes them. Push frames come from a connection's push queues and,
//! when the server owns the connection, are already encoded for the wire.
//!
//! [`PushComposer`] records which composition applies. A standalone actor
//! composes pushes exactly like response frames, so queue output keeps its
//! fragmentation and hook behaviour. A server-owned actor defers composition to
//! the transport sink, which serializes nothing for a push and instead runs the
//! protocol `before_send` hook on the encoded frame.
//!
//! [`Envelope`]: crate::app::Envelope
use tokio::sync::mpsc;
use super::{ConnectionActor, state::ActorState};
use crate::{
app::Packet,
correlation::CorrelatableFrame,
push::{FrameLike, PushPriority},
};
/// Frames taken from the push lanes for one write.
///
/// The priority travels with the frames so a writer that abandons one of them
/// can name the lane it came from. A batch produced by draining both lanes has
/// no single priority and reports none.
pub(crate) struct PendingPushes<P> {
/// Lane that produced the batch, or `None` when both lanes were drained.
pub(crate) priority: Option<PushPriority>,
/// Frames in delivery order, high priority first.
pub(crate) frames: Vec<P>,
}
impl<P> PendingPushes<P> {
/// Build a batch from frames whose lane no longer applies.
const fn mixed(frames: Vec<P>) -> Self {
Self {
priority: None,
frames,
}
}
}
/// Priority lane a push frame was taken from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum PushKind {
/// Urgent pushes polled before every other source.
High,
/// Best-effort pushes subject to fairness yielding.
Low,
}
impl PushKind {
/// Public priority this lane corresponds to.
const fn priority(self) -> PushPriority {
match self {
Self::High => PushPriority::High,
Self::Low => PushPriority::Low,
}
}
}
/// Composition applied to a frame taken from a push queue.
pub(super) enum PushComposer<F, E, P> {
/// Compose pushes the same way the actor composes response frames.
Inline(fn(&mut ConnectionActor<F, E, P>, P, &mut Vec<P>)),
/// Hand pushes to the sink unchanged; the sink owns composition.
Deferred,
}
impl<F, E, P> Clone for PushComposer<F, E, P> {
fn clone(&self) -> Self { *self }
}
impl<F, E, P> Copy for PushComposer<F, E, P> {}
impl<F, E, P> PushComposer<F, E, P> {
/// Compose one push frame into the pending buffer.
fn compose(self, actor: &mut ConnectionActor<F, E, P>, frame: P, pushes: &mut Vec<P>) {
match self {
Self::Inline(compose) => compose(actor, frame, pushes),
Self::Deferred => pushes.push(frame),
}
}
}
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
/// Return `true` while at least one push lane can still yield frames.
#[must_use]
pub(crate) const fn has_open_push_lanes(&self) -> bool {
self.high_rx.is_some() || self.low_rx.is_some()
}
/// Stop accepting new pushes without discarding the frames already queued.
///
/// Shutdown ends the connection's willingness to take pushes. Closing the
/// lanes wakes a producer parked on a full lane, so it learns the lane is
/// closed instead of waiting for capacity that `stop()` has made
/// unreachable.
pub(crate) fn close_push_lanes(&mut self) {
if let Some(rx) = &mut self.high_rx {
rx.close();
}
if let Some(rx) = &mut self.low_rx {
rx.close();
}
}
/// Drop every push lane, discarding the frames still queued.
///
/// A connection that stops while a response is still draining follows the
/// documented rule that a frame waiting in a push queue at that moment is
/// dropped rather than flushed. Dropping the receivers enforces that rule
/// and wakes a producer parked on capacity with `Closed`, so it cannot keep
/// the drain alive by feeding it another frame.
pub(crate) fn discard_push_lanes(&mut self) {
self.high_rx = None;
self.low_rx = None;
}
/// Await the next push, returning `None` once every lane has closed.
///
/// At least one frame is returned when the result is `Some`, so callers
/// never spin on an empty batch. The biased selection keeps urgent pushes
/// ahead of best-effort ones, and a fairness yield pulls one low-priority
/// frame forward after a burst of urgent traffic.
pub(crate) async fn next_pushes(&mut self) -> Option<PendingPushes<P>> {
loop {
if !self.has_open_push_lanes() {
return None;
}
let can_poll_high = self.high_rx.is_some();
let can_poll_low = self.low_rx.is_some();
let high_rx = self.high_rx.as_mut();
let low_rx = self.low_rx.as_mut();
let (kind, received) = tokio::select! {
biased;
res = Self::poll_queue(high_rx), if can_poll_high => (PushKind::High, res),
res = Self::poll_queue(low_rx), if can_poll_low => (PushKind::Low, res),
else => return None,
};
let Some(frame) = received else {
self.close_push_lane(kind);
continue;
};
let mut frames = Vec::new();
self.compose_push(frame, &mut frames);
let is_mixed = match kind {
PushKind::High => self.yield_to_low_after_high(&mut frames),
PushKind::Low => {
self.fairness.reset();
false
}
};
// A fairness yield appends a low-priority frame to a high-priority
// batch, so the batch no longer belongs to one lane. Tagging it
// with the lane that started it would name the wrong priority for
// a frame the writer later abandons.
return Some(PendingPushes {
priority: if is_mixed {
None
} else {
Some(kind.priority())
},
frames,
});
}
}
/// Seal both lanes and take every frame they already hold.
///
/// The connection loop calls this after a clean end-of-stream so frames
/// accepted before the close are still delivered. Frames are returned in
/// priority order, high before low, and the caller writes them. Both lanes
/// close before the first frame is taken, so a producer that pushes while
/// the drain runs sees a closed channel rather than a queue that is never
/// polled again. Either way a push attempted after this returns is rejected
/// with [`crate::push::PushError::Closed`].
///
/// Only frames a lane already holds are taken. A producer that has reserved
/// capacity but is still waiting for a rate permit has not been told its
/// frame was accepted, and one that is never polled again never sends: an
/// awaited reservation would therefore stall the drain before a single
/// accepted frame was written, with nothing but a stop to end the wait.
/// Sealing the lanes first is what makes taking only the buffered frames
/// safe, because a producer still holding a permit observes the closed
/// channel on its next poll and reports
/// [`crate::push::PushError::Closed`] instead of queueing into a lane this
/// drain has finished with.
///
/// Returning the frames rather than writing them lets the caller bound each
/// write by its own idle grace: one deadline for the whole drain would
/// abandon frames the peer was still accepting.
pub(crate) fn take_pending_pushes(&mut self) -> PendingPushes<P> {
// Take both lanes before reading them. A frame pushed while the drain
// runs is then rejected by the now-closed channel instead of being
// accepted into a lane the actor has already stopped polling, which
// would silently lose it.
let mut lanes = [self.high_rx.take(), self.low_rx.take()];
// Taking a lane out of the actor does not close it: the receiver is
// still alive in `lanes`, so a concurrent push would be accepted into a
// lane this drain has already read and then be discarded when `lanes`
// drops. Every lane is therefore closed before any is read, so a push
// arriving during the high-priority pass is rejected rather than queued
// into a low lane that will never be read again.
for receiver in lanes.iter_mut().flatten() {
receiver.close();
}
let mut frames = Vec::new();
for receiver in lanes.iter_mut().flatten() {
self.collect_buffered(receiver, &mut frames);
}
PendingPushes::mixed(frames)
}
/// Compose one push frame according to the actor's configured routing.
pub(super) fn compose_push(&mut self, frame: P, pushes: &mut Vec<P>) {
let composer = self.composer;
composer.compose(self, frame, pushes);
}
/// Pull one low-priority frame forward after urgent traffic.
///
/// Returns whether a low-priority frame joined the batch, which is what
/// makes the batch mixed rather than high-priority.
fn yield_to_low_after_high(&mut self, pushes: &mut Vec<P>) -> bool {
self.fairness.record_high_priority();
if !self.fairness.should_yield_to_low_priority() {
return false;
}
// The temporary state only records closure, which the caller does not
// observe here; lane closure is handled on the next poll.
let mut state = ActorState::new(false, false);
self.try_opportunistic_low_drain(&mut state, pushes)
}
/// Drop a push lane that reported closure.
fn close_push_lane(&mut self, kind: PushKind) {
match kind {
PushKind::High => self.high_rx = None,
PushKind::Low => self.low_rx = None,
}
}
/// Move every frame one sealed lane already holds into `pushes`.
///
/// `try_recv` is what makes this non-blocking: `Disconnected` ends the
/// pass once the buffer is empty, and `Empty` means a producer holds a
/// reservation this drain deliberately does not wait for. A frame sent
/// through that reservation after the receiver drops is lost, which is why
/// the producer checks the lane is still open before it sends and reports
/// [`crate::push::PushError::Closed`] rather than claiming an acceptance
/// the drain will never deliver.
fn collect_buffered(&mut self, receiver: &mut mpsc::Receiver<P>, pushes: &mut Vec<P>) {
while let Ok(frame) = receiver.try_recv() {
self.compose_push(frame, pushes);
}
}
}