Skip to content

fix(round_robin): randomize starting index to distribute load across targets#13

Merged
jlenon7 merged 4 commits into
AthennaIO:developfrom
GyAlves:feature/avt-1890-be-socials-fix-round-robin-api-key-rotation-for-the-socials
Apr 23, 2026
Merged

fix(round_robin): randomize starting index to distribute load across targets#13
jlenon7 merged 4 commits into
AthennaIO:developfrom
GyAlves:feature/avt-1890-be-socials-fix-round-robin-api-key-rotation-for-the-socials

Conversation

@GyAlves

@GyAlves GyAlves commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Round robin strategy now picks a random starting target on first use, then rotates deterministically
  • This ensures new limiter instances (created per-request) distribute load evenly instead of always starting at target[0], which made round_robin behave identically to first_available
  • Adds tests for sequential rotation, concurrent rotation, and distribution across multiple instances

Context

When consumers create a new RateLimiter instance per request (e.g. the socials API), rrIndex always started at 0. Every request would hit target[0] first, making round_robin indistinguishable from first_available.

The fix initializes rrIndex to a random value on first use (rrIndex: number | null = null), so each instance starts at a different target. After that, rotation proceeds deterministically.

GyAlves added 2 commits April 22, 2026 14:05
…targets

- Round robin strategy now picks a random starting target on first use,
  then rotates deterministically. This ensures that new limiter instances
  (created per-request) distribute load evenly instead of always starting
  at target[0], which made round_robin behave like first_available.
- Add tests for sequential rotation, concurrent rotation, and
  distribution across multiple instances.

@jlenon7 jlenon7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, but take a look at the suggestion

Comment thread src/ratelimiter/RateLimiterBuilder.ts Outdated
GyAlves and others added 2 commits April 22, 2026 23:32
@jlenon7 jlenon7 merged commit fc0bc4f into AthennaIO:develop Apr 23, 2026
2 checks passed
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.

2 participants