Skip to content

feat(data-structures/unstable): add RollingCounter#7028

Open
tomas-zijdemans wants to merge 5 commits intodenoland:mainfrom
tomas-zijdemans:sliding-window-counter
Open

feat(data-structures/unstable): add RollingCounter#7028
tomas-zijdemans wants to merge 5 commits intodenoland:mainfrom
tomas-zijdemans:sliding-window-counter

Conversation

@tomas-zijdemans
Copy link
Contributor

This data structure tracks counts across a fixed number of segments using a ring buffer. The caller controls when the window advances by calling rotate(), making it useful for rate limiting (I'm working on a new module for that), circuit breaker ++

@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.84%. Comparing base (8d3e9c3) to head (710936b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7028   +/-   ##
=======================================
  Coverage   93.83%   93.84%           
=======================================
  Files         624      625    +1     
  Lines       49629    49693   +64     
  Branches     8721     8733   +12     
=======================================
+ Hits        46571    46635   +64     
  Misses       2478     2478           
  Partials      580      580           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kt3k
Copy link
Member

kt3k commented Mar 6, 2026

Is it common to call this data structure Sliding Window Counter? Is there any prior example library?

I research a bit about rate limiting algorithms, but this data structure seems more similar to one for sliding window log to me (sliding window counter seems using only 2 fixed windows and combining them with weighted interpolation)

@tomas-zijdemans
Copy link
Contributor Author

tomas-zijdemans commented Mar 6, 2026

Is it common to call this data structure Sliding Window Counter? Is there any prior example library?

I research a bit about rate limiting algorithms, but this data structure seems more similar to one for sliding window log to me (sliding window counter seems using only 2 fixed windows and combining them with weighted interpolation)

I agree. I was looking at a Go package. Renaming to RollingCounter. It's a ring of counters that rotates.

So usage could look like:

// Circuit breaker
const failures = new RollingCounter(10);
const requests = new RollingCounter(10);


// Rate limiter
const permits = new RollingCounter(segmentsPerWindow);

@tomas-zijdemans tomas-zijdemans changed the title feat(data-structures/unstable): add SlidingWindowCounter feat(data-structures/unstable): add RollingCounter Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants