mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
The push gateway's signed-call limiter keeps one counter per attested key, capped at 100,000 keys. Once the map was full, every call from a key not already in it scanned the whole map (once to drop expired windows, again to find the oldest), all while holding the limiter's lock. Under heavy load from many new keys, that serialized each signed call behind a 100,000-entry scan. The limiter now keeps a queue of window starts, oldest first, and evicts from its front. A key whose window restarts leaves a stale entry behind, which eviction skips; the queue is compacted once stale entries make up half of it, so a call stays O(1) amortized. Rate-limit behavior is unchanged, except that a full map now drops one oldest window per new key instead of every expired window at once. The map and the queue share one copy of each key; the queue can reach about twice the map's entries before compaction, so memory stays bounded. Follow-up from #9562. ### Validation | 1,000 new keys on a full map (release build) | Time | | --- | --- | | Before | 277 ms (about 0.28 ms per call, under the lock) | | After | 0.11 ms | - [`a_full_map_drops_the_key_with_the_oldest_window`](https://github.com/block/buzz/blob/push-gateway-limiter-eviction/crates/buzz-push-gateway/src/signed_call_limiter.rs) checks that eviction picks the oldest window. - [`eviction_skips_a_restarted_window`](https://github.com/block/buzz/blob/push-gateway-limiter-eviction/crates/buzz-push-gateway/src/signed_call_limiter.rs) checks that eviction skips a stale entry and keeps a key whose window just restarted. - [`restarted_windows_do_not_grow_the_queue`](https://github.com/block/buzz/blob/push-gateway-limiter-eviction/crates/buzz-push-gateway/src/signed_call_limiter.rs) checks that compaction bounds the queue when one key keeps restarting its window. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>