link: drive request receipt lifecycle

This commit is contained in:
DeFiDude committed 2026-07-24 04:10:34 -06:00
1 parent 6e12be1489
commit 065741d22b
2 files changed
+107 -7

No files matched your search

+56
View File
@@ -1117,6 +1117,11 @@ impl Link {
),
)]
pub fn tick(&mut self) -> LinkAction {
for receipt in &mut self.pending_requests {
receipt.check_timeout();
}
self.pending_requests.retain(|receipt| !receipt.concluded());
match self.state {
LinkState::Pending | LinkState::Handshake => {
if self.request_time.elapsed() > self.establishment_timeout {
@@ -1198,6 +1203,10 @@ impl Link {
fn close(&mut self, reason: CloseReason) {
tracing::debug!(link_id = ?self.link_id, ?reason, "link state -> Closed");
for receipt in &mut self.pending_requests {
receipt.fail();
}
self.pending_requests.clear();
self.state = LinkState::Closed;
self.purge_keys();
}
@@ -2115,6 +2124,53 @@ mod tests {
assert!(initiator.pending_requests.is_empty());
}
#[test]
fn test_tick_expires_pending_request_and_fires_callback() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let (mut initiator, _responder, _) = make_active_link();
initiator
.request("test.timeout", None, Duration::ZERO)
.unwrap();
let fired = Arc::new(AtomicBool::new(false));
let callback_fired = fired.clone();
initiator.pending_requests[0].set_failed_callback(move |_| {
callback_fired.store(true, Ordering::SeqCst);
});
initiator.tick();
assert!(fired.load(Ordering::SeqCst));
assert!(
initiator.pending_requests.is_empty(),
"owning runtime tick must reap concluded request receipts"
);
}
#[test]
fn test_link_close_fails_pending_request() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let (mut initiator, _responder, _) = make_active_link();
initiator
.request("test.close", None, Duration::from_secs(30))
.unwrap();
let fired = Arc::new(AtomicBool::new(false));
let callback_fired = fired.clone();
initiator.pending_requests[0].set_failed_callback(move |_| {
callback_fired.store(true, Ordering::SeqCst);
});
initiator.mark_closed(CloseReason::DestinationClosed);
assert!(fired.load(Ordering::SeqCst));
assert!(initiator.pending_requests.is_empty());
}
#[test]
fn test_request_response_preserves_msgpack_object_bodies() {
use rmpv::Value;
+51 -7
View File
@@ -81,11 +81,12 @@ impl RequestReceipt {
}
pub fn is_timed_out(&self) -> bool {
self.state == RequestState::Sent && self.sent_at.elapsed() > self.timeout
matches!(self.state, RequestState::Sent | RequestState::Receiving)
&& self.sent_at.elapsed() > self.timeout
}
pub fn is_pending(&self) -> bool {
self.state == RequestState::Sent
matches!(self.state, RequestState::Sent | RequestState::Receiving)
}
/// Store the response payload and advance to `ResponseReceived`.
@@ -93,10 +94,17 @@ impl RequestReceipt {
/// Distinct from `deliver()`: `receive_response` can fire after `mark_delivered()`
/// for protocols that ack then stream the payload.
pub fn receive_response(&mut self, data: Vec<u8>) {
if self.state == RequestState::Sent || self.state == RequestState::Delivered {
if matches!(
self.state,
RequestState::Sent | RequestState::Receiving | RequestState::Delivered
) {
self.rtt = Some(self.sent_at.elapsed());
self.response = Some(data);
self.state = RequestState::ResponseReceived;
self.progress = 1.0;
if let Some(cb) = self.callbacks.response.take() {
cb(self);
}
}
}
@@ -152,10 +160,7 @@ impl RequestReceipt {
/// Expire the request if past its deadline, firing the failure callback. Returns `true` if expired.
pub fn check_timeout(&mut self) -> bool {
if self.is_timed_out() {
self.state = RequestState::Failed;
if let Some(cb) = self.callbacks.failed.take() {
cb(self);
}
self.fail();
true
} else {
false
@@ -245,4 +250,43 @@ mod tests {
assert_eq!(receipt.state, RequestState::Failed);
assert!(receipt.response.is_none());
}
#[test]
fn test_receive_response_fires_callback_from_receiving_state() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let fired = Arc::new(AtomicBool::new(false));
let callback_fired = fired.clone();
let mut receipt = RequestReceipt::new([0x55; 32], [0x66; 16], Duration::from_secs(10));
receipt.state = RequestState::Receiving;
receipt.set_response_callback(move |receipt| {
assert_eq!(receipt.state, RequestState::ResponseReceived);
callback_fired.store(true, Ordering::SeqCst);
});
receipt.receive_response(b"resource response".to_vec());
assert!(fired.load(Ordering::SeqCst));
assert_eq!(receipt.get_progress(), 1.0);
}
#[test]
fn test_receiving_request_times_out_and_fires_failure_callback() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let fired = Arc::new(AtomicBool::new(false));
let callback_fired = fired.clone();
let mut receipt = RequestReceipt::new([0x77; 32], [0x88; 16], Duration::ZERO);
receipt.state = RequestState::Receiving;
receipt.set_failed_callback(move |receipt| {
assert_eq!(receipt.state, RequestState::Failed);
callback_fired.store(true, Ordering::SeqCst);
});
assert!(receipt.check_timeout());
assert!(fired.load(Ordering::SeqCst));
assert!(!receipt.is_pending());
}
}