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
//! Writing queued push batches through a streaming connection's transport.
//!
//! A write to a peer that stopped reading can stay pending indefinitely, so
//! every push batch is bounded by the shutdown token and, once the peer can no
//! longer be observed, by the actor's idle grace. The read half keeps being
//! polled while the write runs, because this is the last place that can notice
//! a peer which half-closed and then stopped reading.
//!
//! The work lives apart from [`super::stream_dispatch`] so that module stays
//! the state machine — which event arrived and what it means — while this one
//! owns what a push batch costs to write.
use std::io;
use futures::{Sink, Stream, StreamExt};
use super::{
super::{
PushWriteDiagnostics,
envelope::{Envelope, Packet},
recovery_codec::DecodedItem,
},
frames::InboundExpiry,
push_service::{PushBatch, PushTransport, deliver_pushes, drain_queued},
recovery::hold_or_end,
stream_actions::finish_step,
stream_dispatch::Connection,
stream_handles::LoopState,
stream_observed::ObservedAction,
stream_types::{Event, Step},
};
use crate::{
codec::FrameCodec,
connection::{ConnectionActor, PendingPushes},
frame::FrameMetadata,
message::{DecodeWith, EncodeWith},
request::QueuedBytes,
serializer::Serializer,
};
impl<S, E, R, K, F> Connection<'_, S, E, R, K, F>
where
S: Serializer + FrameMetadata<Frame = Envelope> + Send + Sync,
E: Packet,
R: Stream<Item = io::Result<DecodedItem<F::Frame>>> + Unpin,
K: Sink<F::Frame, Error = io::Error> + Unpin + Send,
F: FrameCodec,
Envelope: DecodeWith<S> + EncodeWith<S>,
{
/// Write one batch of queued pushes through the connection's transport.
///
/// A write to a peer that stopped reading can stay pending indefinitely, so
/// it is bounded by the shutdown token and, once the peer can no longer be
/// observed, by the idle grace. The read half keeps being polled while the
/// write runs, because this is the last place that can notice a peer which
/// half-closed and then stopped reading.
///
/// The write also keeps the inbound read deadline expiring, and an expired
/// streaming assembly is failed here rather than inside the write: the
/// write holds the reader, so the reply a failed body owes can only be
/// forwarded once the batch has been written.
pub(super) async fn write_pushes(
&mut self,
state: &mut LoopState,
pushes: PendingPushes<F::Frame>,
queued: QueuedBytes,
) -> io::Result<Step> {
let read_timeout_ms = self.ctx.read_timeout_ms;
let serializer = self.ctx.serializer;
let codec = self.ctx.codec;
let shutdown = self.shutdown;
let peer_eof = self.peer_eof;
let mut evicted = Vec::new();
let written = {
let Connection {
reader,
sink,
inbound,
actor,
recovery,
..
} = self;
// A read made while the write is pending is buffered for the next
// dispatch, unless it reports the end of the stream: that ends the
// connection now, so a truncation is observed even mid-write.
let held_reader = &mut **reader;
let mut held = held_reader.map(|read: io::Result<DecodedItem<F::Frame>>| {
read.and_then(|item| hold_or_end(item, recovery))
});
let mut transport = PushTransport {
serializer,
codec,
sink,
pipeline: &mut inbound.pipeline,
diagnostics: push_diagnostics::<F>(&**actor),
};
let batch = PushBatch {
frames: pushes.frames,
priority: pushes.priority,
reader: &mut held,
pending_frame: &mut inbound.pending,
expiry: InboundExpiry::new(
&mut inbound.message_assembly,
&mut inbound.read_deadline,
read_timeout_ms,
&mut evicted,
),
peer_eof,
shutdown,
};
deliver_pushes(&mut transport, &mut **actor, batch).await?
};
// The reader is owed the next pass, so a continuously ready push lane
// cannot win every biased select and starve the client's frames.
state.serve_read_first = true;
if !written {
// A push abandoned mid-frame leaves a partial frame on the wire, so
// the exchange cannot be resumed: abort the live body and close
// instead of writing an expiry reply after the broken frame.
state.streams.abort();
return Ok(Step::Closed);
}
if evicted.is_empty() {
return Ok(Step::Continue);
}
let dispatch = self
.dispatch_observed(state, queued, ObservedAction::Expire(evicted))
.await?;
Ok(finish_step(dispatch))
}
/// Deliver frames still queued when the peer stopped writing.
pub(super) async fn drain_pushes(&mut self) -> io::Result<()> {
let serializer = self.ctx.serializer;
let codec = self.ctx.codec;
let shutdown = self.shutdown;
let Connection {
sink,
inbound,
actor,
..
} = self;
let mut transport = PushTransport {
serializer,
codec,
sink,
pipeline: &mut inbound.pipeline,
diagnostics: push_diagnostics::<F>(&**actor),
};
drain_queued(&mut transport, &mut **actor, shutdown).await
}
}
/// Classify one push-lane poll for the loop.
pub(super) fn push_event<T>(pushes: Option<PendingPushes<T>>) -> Event<T> {
pushes.map_or(Event::LanesIdle, Event::Pushes)
}
/// Identity this connection reports when it abandons a push write.
const fn push_diagnostics<F>(
actor: &ConnectionActor<Envelope, (), F::Frame>,
) -> PushWriteDiagnostics
where
F: FrameCodec,
{
PushWriteDiagnostics::new(actor.connection_id(), actor.peer_addr())
}