transport: the announce cap waits the full interval from each send

This commit is contained in:
DeFiDude committed 2026-11-13 18:28:52 -07:00
1 parent 4bfd0fa0fb
commit c6d0abff64
5 files changed
+111 -21

No files matched your search

+4
View File
@@ -335,6 +335,10 @@ Exhaustive literals and matches over these types need updating:
`ic_burst_freq`, `ec_pr_freq` or similar key on an interface is no longer
refused at load. As in upstream, it puts the interface into permanent
limiting (`RNS/Reticulum.py:1001-1010`, `RNS/Interfaces/Interface.py:206`).
- **Announce cap timing.** On an interface with an announce cap, a queued
announce leaves when the wait after the one before it ends, not on the next
250 ms job pass, and each wait counts from that send
(`RNS/Interfaces/Interface.py`, `process_announce_queue`).
- **Stored files.**
- Destination announce stamps persist as `<storage>/stamps/<hash>.dstamp`
(`RNS/Stamps.py:869-875`) and are reused across restarts.
@@ -575,6 +575,10 @@ impl TransportActor {
.announce_queue
.retain(|queued| queued.raw != offered.raw);
sent = true;
if !entry.announce_queue.is_empty() {
let allowed_at = entry.announce_allowed_at;
self.wake_for_announce_at(allowed_at);
}
}
if sent {
self.count_tx_packet();
+27 -18
View File
@@ -235,7 +235,8 @@ impl TransportActor {
self.process_pending_discovery_path_requests(now);
self.process_pending_path_request_admissions(now);
self.process_announce_queues(now);
// The cap counts from the send itself, not from the start of this tick.
self.process_announce_queues(now_f64());
}
/// Transport.py A leaf or shared-instance client lingers
@@ -422,10 +423,10 @@ impl TransportActor {
/// Python `Interface.process_announce_queue` (Interface.py):
/// drop stale entries, send the fewest-hops, oldest entry, and consume it
/// whatever the send outcome. Python re-arms a timer at each `wait_time`;
/// here each tick sends every entry whose slot has passed, chaining slots
/// from the previous one, up to `MAX_ANNOUNCE_DRAIN_PER_TICK`. A zero
/// bitrate empties the queue, as Python's ZeroDivisionError handler does.
/// whatever the send outcome. The next announce is allowed at this send
/// plus the full wait, and the actor wakes then, as Python re-arms a timer
/// for `wait_time`. A zero bitrate empties the queue, as Python's
/// ZeroDivisionError handler does.
pub(super) fn process_announce_queues(&mut self, now: f64) {
let iface_ids: Vec<InterfaceId> = self.interfaces.keys().copied().collect();
for iface_id in iface_ids {
@@ -435,9 +436,25 @@ impl TransportActor {
}
}
}
// An interface that was due and could not send waits for the next tick.
self.announce_drain_at = self
.interfaces
.iter()
.filter(|(id, entry)| {
!entry.announce_queue.is_empty() && !self.offered_announces.contains_key(*id)
})
.map(|(_, entry)| entry.announce_allowed_at)
.filter(|at| *at > now)
.min_by(f64::total_cmp);
}
/// Send at most one queued announce; `true` when another may follow now.
/// Wake the actor to drain announce queues at `at`, unless it wakes sooner.
pub(super) fn wake_for_announce_at(&mut self, at: f64) {
self.announce_drain_at = Some(self.announce_drain_at.map_or(at, |next| next.min(at)));
}
/// Send at most one queued announce; `true` when an entry was discarded
/// unsent and another may follow now.
fn drain_one_queued_announce(&mut self, iface_id: InterfaceId, now: f64) -> bool {
let Some(entry) = self.interfaces.get_mut(&iface_id) else {
return false;
@@ -531,20 +548,12 @@ impl TransportActor {
}
let raw = entry.announce_queue.remove(idx).raw;
// Chain from the previous slot within one tick so throughput follows
// Python's timer; an idle queue starts from now.
let slot = if now - entry.announce_allowed_at < JOB_INTERVAL_MS as f64 / 1000.0 {
entry.announce_allowed_at
} else {
now
};
entry.announce_allowed_at = slot + announce_wait_time(entry, raw.len());
// `announce_allowed_at = now + wait_time` (Interface.py): the full
// wait from this send, never a slot carried over from the last one.
entry.announce_allowed_at = now + announce_wait_time(entry, raw.len());
self.send_queued_announce(iface_id, &raw);
now >= self
.interfaces
.get(&iface_id)
.map_or(f64::INFINITY, |entry| entry.announce_allowed_at)
false
}
/// Python's interface job (Transport.py): re-evaluate both burst
+74 -1
View File
@@ -97,6 +97,9 @@ pub struct TransportActor {
pending_announce_admissions: VecDeque<announce_admission::PendingAnnounce>,
announce_pressure: announce_admission::AnnouncePressure,
offered_announces: HashMap<InterfaceId, announce_admission::OfferedAnnounce>,
/// When the next queued announce may leave. The actor wakes then, as
/// Python arms a timer for each wait (Interface.py, `process_announce_queue`).
announce_drain_at: Option<f64>,
persistence_tx: mpsc::Sender<()>,
persistence_rx: mpsc::Receiver<()>,
path_recovery_tx: mpsc::Sender<crate::path_recovery::PathRecoveryRequest>,
@@ -505,6 +508,7 @@ impl TransportActor {
pending_announce_admissions: VecDeque::new(),
announce_pressure: announce_admission::AnnouncePressure::default(),
offered_announces: HashMap::new(),
announce_drain_at: None,
persistence_tx,
persistence_rx,
path_recovery_tx,
@@ -674,6 +678,11 @@ impl TransportActor {
let mut was_foreground = true;
loop {
// A throttled background actor leaves queued announces to its tick.
let announce_wake = self
.announce_drain_at
.filter(|_| was_foreground || !cfg!(feature = "mobile-throttle"))
.map(|at| Duration::from_secs_f64((at - now_f64()).clamp(0.0, 3600.0)));
tokio::select! {
msg = self.rx.recv() => {
match msg {
@@ -684,6 +693,9 @@ impl TransportActor {
Some(msg) => self.handle_message(msg),
}
}
_ = tokio::time::sleep(announce_wake.unwrap_or_default()), if announce_wake.is_some() => {
self.process_announce_queues(now_f64());
}
(class, msg) = std::future::poll_fn(|cx| self.inbound_rx.poll_recv(cx, &self.inbound_queues)) => {
self.handle_inbound_event(class, msg);
}
@@ -2324,7 +2336,7 @@ impl TransportActor {
continue;
}
// A background lane must see every capped announce, so those queue
// and are offered on the next tick.
// and the actor wakes to offer them.
if entry.announce_queue.is_empty()
&& now > entry.announce_allowed_at
&& entry.bitrate != 0
@@ -2361,6 +2373,8 @@ impl TransportActor {
hops,
raw: shared.clone(),
});
let allowed_at = entry.announce_allowed_at;
self.wake_for_announce_at(allowed_at);
}
}
@@ -8430,6 +8444,65 @@ mod tests {
assert!(actor.interfaces[&1].announce_queue.is_empty());
}
/// Python `Interface.process_announce_queue` (Interface.py): each send
/// sets `announce_allowed_at = now + wait_time`, so a queued announce that
/// leaves late still closes the interface for the full wait, and the
/// actor is woken for the next one when that wait ends.
#[test]
fn queued_announce_waits_the_full_interval_from_each_send() {
let (mut actor, _tx) = TransportActor::new();
let (entry, mut rx) = make_test_interface("full_wait_iface");
actor.interfaces.insert(1, entry);
let mut lens = Vec::new();
for (index, aspect) in ["test.wait.a", "test.wait.b", "test.wait.c"]
.into_iter()
.enumerate()
{
let (raw, dest) = make_valid_announce(aspect, 1);
actor.local_destinations.insert(dest);
actor.broadcast_announce_on_interfaces(&raw, None);
assert_eq!(
rx.try_recv().is_ok(),
index == 0,
"only the first leaves at once"
);
lens.push(raw.len());
}
let wait = |len: usize| {
let entry = &actor.interfaces[&1];
(len as f64 * 8.0) / entry.bitrate as f64 / entry.announce_cap
};
let (wait_b, wait_c) = (wait(lens[1]), wait(lens[2]));
let opened = actor.interfaces[&1].announce_allowed_at;
assert_eq!(
actor.announce_drain_at,
Some(opened),
"queueing arms the wake for the end of the first wait"
);
// The second leaves a little late, as after a busy actor pass.
let late = opened + 0.2;
actor.process_announce_queues(late);
assert!(rx.try_recv().is_ok());
assert_eq!(actor.interfaces[&1].announce_allowed_at, late + wait_b);
assert_eq!(actor.announce_drain_at, Some(late + wait_b));
actor.process_announce_queues(late + wait_b - 0.001);
assert!(
rx.try_recv().is_err(),
"no gap is shorter than the wait after the send before it"
);
actor.process_announce_queues(late + wait_b);
assert!(rx.try_recv().is_ok());
assert_eq!(
actor.interfaces[&1].announce_allowed_at,
late + wait_b + wait_c
);
assert!(actor.interfaces[&1].announce_queue.is_empty());
assert_eq!(actor.announce_drain_at, None, "an empty queue arms no wake");
}
/// Python 1.3.8 Transport.py keeps one queued announce per
/// destination and replaces it only when the wire emission time advances.
#[test]
+2 -2
View File
@@ -274,8 +274,8 @@ pub const QUEUED_ANNOUNCE_LIFE: f64 = 10_800.0;
/// Maximum number of queued announces per interface.
pub const MAX_QUEUED_ANNOUNCES: usize = 4096;
/// Queued announces one interface may send in one maintenance tick; bounds
/// actor work on fast links.
/// Queued announces one interface may discard unsent in one drain pass, on
/// the way to the one it sends; bounds actor work.
pub(crate) const MAX_ANNOUNCE_DRAIN_PER_TICK: usize = 256;
/// Grace period in seconds after the first interface registration before