Skip to content

fix: bound enrichment caches to prevent unbounded memory growth (LAN-145) - #115

Merged
lance0 merged 1 commit into
masterfrom
fix/bounded-enrichment-caches
Jul 6, 2026
Merged

lance0 merged 1 commit into
masterfrom
fix/bounded-enrichment-caches

Conversation

@lance0

@lance0 lance0 commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Summary

Closes LAN-145 (#11: unbounded enrichment caches).

All four enrichment caches (ASN, rDNS, Geo, IX) were unbounded HashMap<IpAddr, _>s — they inserted entries with timestamps but never evicted. Long-running sessions accumulated memory indefinitely.

Changes

Shared helper (src/lookup/mod.rs):

  • prune_cache() — two-phase eviction:
    1. Drop TTL-expired entries (cached_at + ttl elapsed)
    2. If still over ENRICHMENT_CACHE_CAP (10,000), evict oldest by cached_at
  • ENRICHMENT_CACHE_CAP = 10_000 — 64 max TTL × ~3 responders/hop × 10 targets ≈ 2,000 IPs; cap gives 5× headroom

Per-module — call prune_cache after each insert:

  • src/lookup/asn.rs — ASN cache
  • src/lookup/rdns.rs — rDNS cache
  • src/lookup/geo.rs — GeoIP cache
  • src/lookup/ix.rs — IX per-IP cache; merged dual ip_cache + ip_cache_times maps into single HashMap<IpAddr, IpCacheEntry> to eliminate the dual-map sync hazard and simplify eviction

Tests

4 new tests in lookup::tests:

  • test_prune_removes_expired_entries — phase 1: TTL expiry
  • test_prune_keeps_all_when_under_and_fresh — no-op when under cap
  • test_prune_phase2_evicts_oldest — phase 2: over-cap evicts oldest 3 (10,003 entries → 10,000, oldest 3 removed)
  • test_prune_empty_cache_is_noop — empty cache

All 593 tests pass, clippy clean.

…145)

Add prune_cache helper in lookup/mod.rs that performs two-phase eviction:
1. Drop TTL-expired entries (cached_at + ttl has elapsed)
2. If still over ENRICHMENT_CACHE_CAP (10,000), evict oldest by cached_at

Apply to all four enrichment caches:
- ASN (src/lookup/asn.rs): prune after insert in lookup()
- rDNS (src/lookup/rdns.rs): prune after insert in reverse_lookup()
- Geo (src/lookup/geo.rs): prune after insert in lookup()
- IX (src/lookup/ix.rs): prune after insert in lookup(), merge dual
  ip_cache + ip_cache_times maps into single HashMap<IpAddr, IpCacheEntry>
  to eliminate the dual-map sync hazard and simplify eviction

CAP rationale: 64 max TTL × ~3 responders/hop × 10 targets = ~2,000 IPs.
10,000 gives 5× headroom while preventing pathological growth.
@lance0
lance0 merged commit 0c648f1 into master Jul 6, 2026
5 checks passed
@lance0
lance0 deleted the fix/bounded-enrichment-caches branch July 6, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant