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
use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll, ready},
time::Instant,
};
use bytes::Bytes;
use futures::{Future, SinkExt, StreamExt};
use rustc_hash::FxHashMap;
use tokio::{
sync::{mpsc, oneshot},
time::Interval,
};
use super::{ReqError, ReqOptions, SendCommand};
use crate::req::{SocketState, conn_manager::ConnManager};
use msg_common::span::{EnterSpan as _, SpanExt as _, WithSpan};
use msg_transport::{Address, Transport};
use msg_wire::{
compression::{Compressor, try_decompress_payload},
reqrep,
};
/// The request socket driver. Endless future that drives
/// the socket forward.
pub(crate) struct ReqDriver<T: Transport<A>, A: Address> {
/// Options shared with the socket.
pub(crate) options: Arc<ReqOptions>,
/// State shared with the socket.
pub(crate) socket_state: SocketState<T::Stats>,
/// ID counter for outgoing requests.
pub(crate) id_counter: u32,
/// Commands from the socket.
pub(crate) from_socket: mpsc::Receiver<SendCommand>,
/// Connection manager that handles connection lifecycle.
pub(crate) conn_manager: ConnManager<T, A>,
/// The timer for the write buffer linger.
pub(crate) linger_timer: Option<Interval>,
/// The single pending outgoing message waiting to be sent.
pub(crate) pending_egress: Option<WithSpan<reqrep::Message>>,
/// The currently pending requests waiting for a response.
pub(crate) pending_requests: FxHashMap<u32, WithSpan<PendingRequest>>,
/// Interval for checking for request timeouts.
pub(crate) timeout_check_interval: Interval,
/// Optional message compressor. This is shared with the socket to keep
/// the API consistent with other socket types (e.g. `PubSocket`)
pub(crate) compressor: Option<Arc<dyn Compressor>>,
/// An unique ID for this driver.
pub(crate) id: usize,
/// A span to use for general purpose notifications, not tied to a specific path.
pub(crate) span: tracing::Span,
}
/// A pending request that is waiting for a response from the peer.
pub(crate) struct PendingRequest {
/// The timestamp when the request was sent.
start: Instant,
/// The sender to send the peer response back to the user.
sender: oneshot::Sender<Result<Bytes, ReqError>>,
}
impl<T, A> ReqDriver<T, A>
where
T: Transport<A>,
A: Address,
{
/// Handle an incoming message from the connection.
fn on_message(&mut self, msg: reqrep::Message) {
let Some(pending) = self.pending_requests.remove(&msg.id()).enter() else {
tracing::warn!(parent: &self.span, msg_id = msg.id(), "received response for unknown request id");
return;
};
let rtt = pending.start.elapsed();
tracing::debug!(msg_id = msg.id(), ?rtt, "received response");
let size = msg.size();
let compression_type = msg.header().compression_type();
let mut payload = msg.into_payload();
// decompress the response
match try_decompress_payload(compression_type, payload) {
Ok(decompressed) => payload = decompressed,
Err(e) => {
tracing::error!(?e, "failed to decompress response payload");
let _ =
pending.inner.sender.send(Err(ReqError::Wire(reqrep::Error::Decompression)));
return;
}
}
if pending.inner.sender.send(Ok(payload)).is_err() {
tracing::error!("failed to send peer response back, dropped receiver");
}
// Update stats
self.socket_state.stats.specific.update_rtt(rtt.as_micros() as usize);
self.socket_state.stats.specific.increment_rx(size);
}
/// Handle an incoming command from the socket frontend.
fn on_send(&mut self, cmd: SendCommand) {
let SendCommand { mut message, response } = cmd;
let start = Instant::now();
// We want ot inherit the span from the socket frontend
let span =
tracing::info_span!(parent: &message.span, "send", driver_id = format!("req-{}", self.id)).entered();
// Compress the message if it's larger than the minimum size
let size_before = message.payload().len();
if size_before > self.options.min_compress_size &&
let Some(ref compressor) = self.compressor
{
let start = Instant::now();
if let Err(e) = message.compress(compressor.as_ref()) {
tracing::error!(?e, "failed to compress message");
}
tracing::debug!(
size_before,
size_after = message.payload().len(),
elapsed = ?start.elapsed(),
"compressed message",
);
}
let msg = message.inner.into_wire(self.id_counter);
let msg_id = msg.id();
self.id_counter = self.id_counter.wrapping_add(1);
self.pending_egress = Some(msg.with_span(span.clone()));
self.pending_requests
.insert(msg_id, PendingRequest { start, sender: response }.with_span(span));
}
/// Check for request timeouts and notify the sender if any requests have timed out.
/// This is done periodically by the driver.
fn check_timeouts(&mut self) {
let now = Instant::now();
let timed_out_ids = self
.pending_requests
.iter()
.filter_map(|(&id, request)| {
if now.duration_since(request.start) > self.options.timeout {
Some(id)
} else {
None
}
})
.collect::<Vec<_>>();
for id in timed_out_ids {
if let Some(pending_request) = self.pending_requests.remove(&id) {
let _ = pending_request.into_inner().sender.send(Err(ReqError::Timeout));
}
}
}
}
impl<T, A> Future for ReqDriver<T, A>
where
T: Transport<A>,
A: Address,
{
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let span = this.span.clone();
loop {
// Handle connection management: connection task, backoff, and retry logic
let channel = match this.conn_manager.poll(cx) {
Poll::Ready(Some(channel)) => channel,
Poll::Ready(None) => return Poll::Ready(()),
Poll::Pending => return Poll::Pending,
};
// Check for incoming messages from the socket
match channel.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(msg))) => {
this.on_message(msg);
continue;
}
Poll::Ready(Some(Err(e))) => {
let _g = span.enter();
tracing::warn!(
?e,
"failed to read from connection, resetting connection state"
);
// set the connection to inactive, so that it will be re-tried
this.conn_manager.reset_connection();
continue;
}
Poll::Ready(None) => {
let _g = span.enter();
tracing::warn!("connection closed, resetting connection state");
// set the connection to inactive, so that it will be re-tried
this.conn_manager.reset_connection();
continue;
}
Poll::Pending => {}
}
// Try to send the pending egress message if we have one.
// We only hold a single pending message here; the channel serves as the actual queue.
// This pattern ensures we respect backpressure and don't accumulate unbounded messages.
if let Some(msg) = this.pending_egress.take().enter() {
let size = msg.size();
tracing::debug!("Sending msg {}", msg.id());
// Write the message to the buffer.
// FIXME: handle restoring message in pending_egress if send/flush fails
match channel.start_send_unpin(msg.inner) {
Ok(_) => {
this.socket_state.stats.specific.increment_tx(size);
}
Err(e) => {
tracing::error!(err = ?e, "Failed to send message to socket");
// set the connection to inactive, so that it will be re-tried
this.conn_manager.reset_connection();
continue;
}
}
}
// Flush if write buffer is full according to configured `write_buffer_size`
if channel.write_buffer().len() >= this.options.write_buffer_size {
if let Poll::Ready(Err(e)) = channel.poll_flush_unpin(cx) {
tracing::error!(err = ?e, "Failed to flush connection");
this.conn_manager.reset_connection();
continue;
}
if let Some(ref mut linger_timer) = this.linger_timer {
// Reset the linger timer.
linger_timer.reset();
}
}
// Flush if we have some data and `linger_timer` is ready
if let Some(ref mut linger_timer) = this.linger_timer &&
!channel.write_buffer().is_empty() &&
linger_timer.poll_tick(cx).is_ready() &&
let Poll::Ready(Err(e)) = channel.poll_flush_unpin(cx)
{
tracing::error!(err = ?e, "Failed to flush connection");
this.conn_manager.reset_connection();
}
// Check for request timeouts
while this.timeout_check_interval.poll_tick(cx).is_ready() {
this.check_timeouts();
}
// Check for outgoing messages from the socket handle.
// Only poll for new requests when pending_egress is empty AND we're under HWM to
// maintain backpressure.
let under_hwm = this.pending_requests.len() < this.options.max_pending_requests;
if this.pending_egress.is_none() && under_hwm {
match this.from_socket.poll_recv(cx) {
Poll::Ready(Some(cmd)) => {
this.on_send(cmd);
continue;
}
Poll::Ready(None) => {
tracing::debug!(
"socket dropped, shutting down backend and flushing connection"
);
if let Some(channel) = this.conn_manager.active_connection() {
let _ = ready!(channel.poll_close_unpin(cx));
}
return Poll::Ready(());
}
Poll::Pending => {}
}
}
return Poll::Pending;
}
}
}