Repository navigation
Bound per-hop responders, the live event log and PeeringDB reads - #178
Merged
Merged
Conversation
Every distinct source address that answered at a hop got a permanent ResponderStats entry (~5 KB with its sample buffers). Per-packet balancing behind CGNAT, or a box replying from spoofed sources, grew long daemon, Prometheus and TUI runs without bound and slowed every render, scrape and enrichment pass over the map. A hop now tracks at most 64 responders. When a new one arrives at the cap, the responder with the fewest replies is dropped (never the primary or the flap-tracking primary), and flows forget it too. Hop sent/received/timeouts are hop-level, so loss is unchanged, as are the primary and its RTT. The eviction count is exported as responders_evicted (JSON), ttl_hop_responders_evicted_total (Prometheus) and shown in hop details.
A live TUI session recorded every probe event for e export and --animate for the whole session (about 5 MB an hour per target), and e copied all of it under the session lock. Headless runs were already bounded: only JSON streams record, and they drain every tick. record_event now keeps the newest 100,000 events, dropping the oldest quarter at once when full so the shift stays amortized O(1), and counts what it dropped in dropped_events (serialized when non-zero, cleared by reset). Animated replay of a truncated recording says so and starts at the first kept event instead of idling through the missing part.
The three PeeringDB limit=0 dumps were read with .json() and the IX cache with read_to_string, so a broken or hostile response, or a replaced ix_cache.json, was read into memory whole. Real dumps are under 1.5 MB. Responses now stream through a 16 MiB cap (checked against Content-Length first) and the cache is read through File::take, like replay loading; an oversized body or cache is refused with an error naming the limit. The three per-endpoint fetchers collapse into one generic helper.
# Conflicts: # CHANGELOG.md # src/state/session.rs
lance0
marked this pull request as ready for review
November 14, 2026 20:38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bounds three structures that grew for the whole run in the modes that run for days (
--daemon,--prometheus, long TUI sessions). One commit each.1. Per-hop responder map
Symptom. Every distinct source address that answered at a hop got a permanent
ResponderStatsentry, about 5 KB each because of its pre-allocated sample buffers. Per-packet balancing behind CGNAT, a load balancer with many source addresses, or a box answering each probe from a spoofed source grew memory steadily, and slowed everything that walks the map: every TUI snapshot clones it, every Prometheus scrape and ECMP classification iterates it, andupdate_primaryscans it on every reply. Each new address was also handed to the rDNS/ASN/GeoIP/IX workers.Root cause.
Hop::record_response_with_extensionsdidresponders.entry(ip).or_insert_with(...)with no bound. Onlyreset_statsever cleared it. #155 did not touch this; it only stopped recording replay events in headless runs.Fix. At most
Hop::MAX_RESPONDERS = 64per hop. When a new address arrives at the cap, the responder with the fewest replies is dropped, never the primary or the flap-tracking primary. I chose fewest replies over least recently seen on purpose: one-off sources then churn through a single slot instead of pushing out real ECMP paths, which LRU would let them do. Each flow'sresponder_countsforgets the evicted address too, and the flow primary is recomputed if needed, so flow maps stay within the same bound.What does not change:
sent/received/timeouts, and therefore loss, are hop-level counters.receivedresponder and is never evicted, so the primary, its RTT stats and route-flap tracking are unchanged.primary.received / hop.received. Both values are unaffected, so classification is the same as on master.The eviction count is visible as
.hops[].responders_evictedin JSON (present only when non-zero), thettl_hop_responders_evicted_totalPrometheus counter, and an "Evicted N low-count responders (keeps 64)" line in the hop details popup.Enrichment workers take their addresses only from this map (
collect_lookup_batch), so the cap also bounds what they are handed. Each tick was already limited toMAX_CONCURRENT_LOOKUPS.2. Live probe-event log
Symptom. A live TUI session records every probe event for
eexport and--animate, about 30 events/s on a 30-hop path, or roughly 5 MB per hour per target.eclones all of it under the session read lock.Already bounded on master (#155): headless runs record only for
--stream-json, which drains every tick. Batch modes require-c, so their length is fixed. The TUI was the remaining unbounded case.Fix.
record_eventkeeps the newestSession::MAX_EVENT_LOG = 100_000events, about an hour of a 30-hop path at the default interval and about 5 MB. When the log is full it drops the oldest quarter at once, so the shift is amortized O(1). It adds what it dropped to a newdropped_eventsfield, which is serialized when non-zero and cleared byr. When--animatereplays a truncated recording, it prints a note and starts at the first kept event instead of waiting out an hour of missing time. Final hop statistics in the export cover the whole run. Batch--jsonstill carries a replayableeventsarray.Because the log is now bounded,
estill clones the session (a few MB at most) rather than switching to a separate snapshot path.3. PeeringDB response and cache size
Symptom. The three
limit=0PeeringDB dumps were read with.json(), and~/.cache/ttl/peeringdb/ix_cache.jsonwas read withread_to_string, both with no size bound.Fix. Responses are streamed through a 16 MiB cap, checked against
Content-Lengthfirst. The cache file is read throughFile::take, the same pattern asread_session_limited. The real dumps are under 1.5 MB today (ix 1.45 MB, ixlan 0.39 MB, ixpfx 0.49 MB), and the cache is about 0.37 MB. An oversized body or cache is refused with an error that names the limit. The three per-endpoint fetchers are merged into one generic helper. There is no gzip feature, so there is no decompression path to bound.Tests
responder_map_stays_bounded_without_changing_hop_stats: two real per-packet paths plus 640 one-off sources and some timeouts. It checks that the map stays at 64, the eviction count is exact, the real alternate path survives, the primary and its count are unchanged, loss stays at 20%, there are no false flaps, flow maps stay bounded, classification is stillPerPacket, and the JSON field is present only when non-zero.live_event_log_keeps_the_newest_events_and_counts_the_rest: records 3x the cap. It checks the bound, thatdropped + kept == total, that the oldest events are dropped in order, thatdropped_eventsserializes and survives the render snapshot, and that reset clears it.evicted_responders_are_reported: the hop details popup shows the eviction line only when there were evictions.oversized_peeringdb_bodies_are_refused: a localhost HTTP server sends an over-capContent-Length, an over-cap chunked body with no length, and an at-cap body that is accepted.oversized_cache_file_is_refused: a valid cache padded past 16 MiB is refused.Fails on master: the new tests reference new fields, so they do not compile there. To confirm the behavior, I disabled the eviction call and the drain on this branch. The two state tests then fail (responder map at 642 instead of 64; event log over the cap). The cache test's padded file is valid JSON that master loads whole.
Gates:
cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo clippy --all-targets --no-default-features -- -D warningsandcargo testall pass. The first commit also passescargo test --libon its own.Live check (docker, NET_RAW, 1.1.1.1, 8 hops)
--daemon --prometheusfor about 75 s, branch and master side by side. Hops 2 to 8 match exactly (78 sent each, the same timeouts, 1 responder per hop).ttl_hop_responders_evicted_totalis 0 on every hop, and the scrape is 319 lines against 297 on master (the new counter, one per hop). RSS was 5.8 MiB on the branch and 5.3 MiB on master. Hop 1 is the home gateway, which ICMP-rate-limits; its timeouts differed (54 vs 18) because both daemons shared its budget.-c 15 --reportruns per build, each run alone. Hops 2 to 8 were identical in every run. Hop 1 loss moved between 0% and 93% on both builds (master 6.7/0/53/0, branch 6.7/93/93/0), so that variation is rate limiting, not this change.--animate: a real-c 3recording, with offsets shifted by an hour anddropped_events: 75000, printed the note and played all 8 hops within seconds at--speed 1.Not verified