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
//! Forwarding a legacy route's client frames without parking the connection
//! reader on request credit; the queue itself is described in
//! `forwarding_waiting_requests`.
use super::*;
use crate::forwarding::{ChannelFlowClosed, LegacyArrival};
impl ForwardBackend {
/// Forward, queue, or (only when the route's queue is full) wait to queue
/// a client frame on a route opened without `busy_refusals`.
pub(super) async fn offer_to_legacy_route(
&self,
frame: Frame,
route: Arc<RouteBinding>,
) -> Result<(), RouterError> {
let mut frame = frame;
loop {
match route.flow.offer_legacy(frame) {
LegacyArrival::Forward { frame, credited } => {
return self.forward_to_module(frame, &route, credited).await;
}
LegacyArrival::Queued { start_forwarding } => {
if start_forwarding {
tokio::spawn(self.clone().forward_waiting(route));
}
return Ok(());
}
LegacyArrival::Full(refused) => {
frame = refused;
self.forwarding
.counters_ref()
.observe_reader_wait(
|| (route.module_id.clone(), principal_label(&route.principal)),
"legacy_credit",
route.flow.waiting_room(),
)
.await;
}
LegacyArrival::Closed(frame) => {
let header = &frame.header;
return Err(self.legacy_credit_error(
&route,
header.channel,
header.epoch,
header.corr,
ChannelFlowClosed,
)?);
}
}
}
}
/// Move a route's waiting frames to the module in order, each request once
/// it has credit. A frame that cannot go gets the ERROR the connection
/// reader would have sent for it. Every wait here also ends when the
/// route's credit window closes, as it does when the route is released or
/// its client connection is cleaned up: nothing then reaches the module,
/// and a client that stopped reading cannot hold this task.
async fn forward_waiting(self, route: Arc<RouteBinding>) {
while let Some(frame) = route.flow.next_waiting() {
let (ty, channel, epoch, corr) = (
frame.header.ty,
frame.header.channel,
frame.header.epoch,
frame.header.corr,
);
let error = tokio::select! {
biased;
() = route.flow.closed() => {
if ty != FrameType::Request {
continue;
}
match self.legacy_credit_error(&route, channel, epoch, corr, ChannelFlowClosed) {
Ok(error) | Err(error) => error,
}
}
result = self.forward_waiting_frame(frame, &route) => match result {
Ok(()) => continue,
Err(error) => error,
},
};
let Some(error_frame) = error.to_error_frame() else {
continue;
};
tokio::select! {
biased;
() = route.flow.closed() => {
// Best effort only once the route is gone: never wait.
let _ = route.client_sink.try_send(error_frame);
}
_ = route.client_sink.send(error_frame.clone()) => {}
}
}
}
async fn forward_waiting_frame(
&self,
frame: Frame,
route: &RouteBinding,
) -> Result<(), RouterError> {
let credited = frame.header.ty == FrameType::Request;
if credited {
let subscription = frame.header.flags.is_subscription();
if let Err(closed) = route
.flow
.acquire_tagged(frame.header.corr, subscription)
.await
{
let header = &frame.header;
return Err(self.legacy_credit_error(
route,
header.channel,
header.epoch,
header.corr,
closed,
)?);
}
}
self.forward_to_module(frame, route, credited).await
}
/// The error for a request whose route closed its credit window before
/// the request could be forwarded.
fn legacy_credit_error(
&self,
route: &RouteBinding,
channel: u16,
epoch: u32,
corr: u64,
err: ChannelFlowClosed,
) -> Result<RouterError, RouterError> {
self.forwarding.warn_frame_drop(
route.client_connection_id,
channel,
epoch,
"client_to_module",
"endpoint_gone",
);
// `module_reloading` here is answered BEFORE the frame is forwarded,
// so the module never sees the request and callers may re-dispatch it
// after reopening the route. That is a wire guarantee documented on
// `error_codes::MODULE_RELOADING`; do not emit this code for a request
// that already reached the module's queue.
if self
.forwarding
.endpoint_is_draining(route.module_endpoint)
.map_err(RouterError::Forwarding)?
{
return Ok(RouterError::route_error_with_epoch(
channel,
epoch,
corr,
"module_reloading",
format!("module endpoint for route channel {channel} is reloading"),
));
}
Ok(RouterError::backend_with_epoch(
channel,
epoch,
corr,
format!("{err} for route channel {channel}"),
))
}
}