Skip to content

Commit 7895f28

Browse files
committed
chore(pegboard): log explicit gc reason for pending requests
1 parent 2769824 commit 7895f28

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

engine/packages/pegboard-gateway/src/shared_state.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,14 @@ impl SharedState {
401401
}
402402

403403
async fn gc(&self) {
404+
#[derive(Debug)]
405+
enum MsgGcReason {
406+
/// Any tunnel message not acked (TunnelAck)
407+
MessageNotAcked,
408+
/// WebSocket pending messages (ToServerWebSocketMessageAck)
409+
WebSocketMessageNotAcked,
410+
}
411+
404412
let mut interval = tokio::time::interval(GC_INTERVAL);
405413
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
406414

@@ -410,32 +418,44 @@ impl SharedState {
410418
let now = Instant::now();
411419

412420
self.in_flight_requests
413-
.retain_async(|_, req| {
421+
.retain_async(|request_id, req| {
414422
if req.msg_tx.is_closed() {
415423
return false;
416424
}
417425

418-
let mut keep = true;
419-
420-
if let Some(earliest_pending_msg) = req.pending_msgs.first() {
421-
keep = now.duration_since(earliest_pending_msg.send_instant)
422-
> MESSAGE_ACK_TIMEOUT;
423-
}
426+
let reason = 'reason: {
427+
if let Some(earliest_pending_msg) = req.pending_msgs.first() {
428+
if now.duration_since(earliest_pending_msg.send_instant)
429+
<= MESSAGE_ACK_TIMEOUT
430+
{
431+
break 'reason Some(MsgGcReason::MessageNotAcked);
432+
}
433+
}
424434

425-
if let Some(hs) = &req.hibernation_state {
426-
if let (true, Some(earliest_pending_ws_msg)) =
427-
(keep, hs.pending_ws_msgs.first())
435+
if let Some(hs) = &req.hibernation_state
436+
&& let Some(earliest_pending_ws_msg) = hs.pending_ws_msgs.first()
428437
{
429-
keep = now.duration_since(earliest_pending_ws_msg.send_instant)
430-
> MESSAGE_ACK_TIMEOUT;
438+
if now.duration_since(earliest_pending_ws_msg.send_instant)
439+
<= MESSAGE_ACK_TIMEOUT
440+
{
441+
break 'reason Some(MsgGcReason::WebSocketMessageNotAcked);
442+
}
431443
}
432-
}
433444

434-
if !keep {
445+
None
446+
};
447+
448+
if let Some(reason) = &reason {
449+
tracing::debug!(
450+
request_id=?Uuid::from_bytes(*request_id),
451+
?reason,
452+
"gc collecting in flight request"
453+
);
435454
let _ = req.msg_tx.send(TunnelMessageData::Timeout);
436455
}
437456

438-
keep
457+
// Return true if the request was not gc'd
458+
reason.is_none()
439459
})
440460
.await;
441461
}

0 commit comments

Comments
 (0)