Skip to content

fix: tick most RateCounters every 5 seconds instead of every second#1787

Merged
edgurgel merged 2 commits intomainfrom
fix/rate-counter-tick
Apr 6, 2026
Merged

fix: tick most RateCounters every 5 seconds instead of every second#1787
edgurgel merged 2 commits intomainfrom
fix/rate-counter-tick

Conversation

@edgurgel
Copy link
Copy Markdown
Member

@edgurgel edgurgel commented Apr 3, 2026

What kind of change does this PR introduce?

First we fix how avg is calculated to always reflect per second as it's easier to reason about

Then we increase the tick for most rate counters to not happen so frequently.

Pros:

  • Less work happening every second
  • The 5-second window acts as an implicit moving average over a wider time slice.

Cons:

  • A tenant can exceed their rate limit for up to 5 seconds before limit.triggered flips and enforcement kicks in. With a 1-second tick violations are caught within ~1 second. This is the most significant downside for enforcement.

For a tenant sending a constant rate of events, both configurations produce the exact same avg value. The difference only matters during ramp-up, ramp-down, or abuse patterns.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates RateCounter to normalize its avg calculation to events per second (independent of the tick interval), and then increases the tick interval for most tenant per-second rate counters from 1s to 5s to reduce per-second work.

Changes:

  • Normalize RateCounter.avg by dividing by the configured tick duration (so avg is always per-second).
  • Update tenant rate counter configs (joins/events/db events/presence events) to tick every 5 seconds with a 60-second rolling window (12 buckets).
  • Adjust and add tests to validate avg normalization and accommodate the new tick interval.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
lib/realtime/rate_counter/rate_counter.ex Normalizes avg to events/second; updates module docs.
lib/realtime/tenants.ex Changes most tenant per-second counters to 5s tick + 12 buckets.
test/realtime/rate_counter/rate_counter_test.exs Updates existing limit test timing and adds avg normalization tests.
test/realtime_web/channels/realtime_channel/presence_handler_test.exs Increases event counts in rate-limit tests to match 5s tick behavior.
test/integration/rt_channel/connection_lifecycle_test.exs Increases join attempts to trigger join-rate limit under 5s tick.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/realtime/rate_counter/rate_counter_test.exs
Comment thread lib/realtime/tenants.ex
Comment thread lib/realtime/tenants.ex
Comment thread lib/realtime/tenants.ex
Comment thread lib/realtime/tenants.ex
Comment thread lib/realtime/rate_counter/rate_counter.ex
@edgurgel edgurgel merged commit 87d663d into main Apr 6, 2026
12 checks passed
@edgurgel edgurgel deleted the fix/rate-counter-tick branch April 6, 2026 23:23
@realtime-release-bot
Copy link
Copy Markdown

🎉 This PR is included in version 2.80.12 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants