Fix pubsub TTL cache eviction - #203
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the pubsub TTL cache implementation to evict entries based on their expiration timestamps (rather than key ordering), improves thread-safety around cache access/sweeping/disposal, and adds regression tests for eviction and lazy expiration behavior.
Changes:
- Replaced key-ordered eviction with expiration-time-based eviction (
RemoveExpired) and switched the backing store to aDictionary. - Made cache reads (
Contains,TryGet,ToList) honor TTL even if the sweeper hasn’t run yet. - Added NUnit regression tests covering out-of-order keys and lazy expiration.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs | Reworks TTL cache storage/eviction and adds synchronized access + cancellation-based sweeper disposal. |
| src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs | Adds regression tests for eviction correctness and lazy expiration behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:30
- This test name implies it runs before the background sweeper executes, but with the current implementation the sweeper interval is min(5s, ttl). With ttl=25ms, the sweeper is expected to run very quickly, so the name is misleading.
public void ExpiredEntries_AreNotReturned()
src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:54
- Similar to the first test, the 25ms TTL and 60ms sleep are very tight for a time-based test; on a slow agent the replacement add/assert can race with timing jitter. Increasing the TTL and sleep margin will reduce flakiness.
using TtlCache<MessageId, string> cache = new(25);
MessageId id = new([0x01]);
cache.Add(id, "expired");
Thread.Sleep(60);
cache.Add(id, "replacement");
53cf5ea to
72abfc0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
Suppressed comments (2)
src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:36
- Thread.Sleep makes this test timing-dependent and can be flaky on slow CI agents. Use a bounded retrying assertion to wait for expiry instead of sleeping a fixed duration.
Thread.Sleep(750);
src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:54
- Thread.Sleep makes this test timing-dependent and can be flaky under load. Prefer waiting for expiry with a bounded retrying assertion, then proceed to add the replacement entry.
Thread.Sleep(750);
cache.Add(id, "replacement");
Summary
Pipeline repair
This branch also includes the shared SIPSorcery security dependency update required for CI restore. It aligns the WebRTC certificate integration and is tracked independently in #208.
Validation