Skip to content

Add rate limiting with retry logic to flashlight API queries#140

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/add-ratelimiting-to-queries
Draft

Add rate limiting with retry logic to flashlight API queries#140
Copilot wants to merge 4 commits intomainfrom
copilot/add-ratelimiting-to-queries

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 20, 2026

Flashlight API queries lacked rate limiting and retry capabilities, risking throttling errors during high-volume usage.

Implementation

Rate limiter utility (src/helpers/rateLimiter.ts):

  • Module-scoped PQueue instances with configurable concurrency and interval limits
  • retryOnRateLimit(): Exponential backoff retry wrapper (5 attempts max, 1s → 10s delays)
  • checkRateLimit(): Pre-execution queue capacity validation (prevents unbounded growth at 10x concurrency)

Query updates:

  • Added rate limiter constants to all flashlight queries (uuid.ts, username.ts, sessions.ts, history.ts, wrapped.ts)
  • Configured at 10 requests/second per endpoint
  • Wrapped query functions with automatic retry logic

Example

// Module-scoped rate limiter
const uuidRateLimiter = createRateLimiter({
    concurrency: 1,
    interval: 1000,
    intervalCap: 10,
});

// Query wrapped with retry logic
queryFn: async () => {
    return retryOnRateLimit(
        async () => {
            const response = await fetch(/* ... */);
            // ... handle response
        },
        uuidRateLimiter,
    );
}

Rate limit errors automatically retry with exponential backoff rather than failing immediately.

Original prompt

Add ratelimiting to all the queries to flashlight in the queries folder. Above each query definition, add a module scopes constant containing the rate limiter. In the query ensure the limiter has a limit left, and if not throw an error, then add a retry function that retries up to 5 times on ratelimiting errors. Use p-queue and throwontimeout


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Jan 20, 2026

Deploying rainbow with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9e89868
Status: ✅  Deploy successful!
Preview URL: https://7efdc746.rainbow-ctx.pages.dev
Branch Preview URL: https://copilot-add-ratelimiting-to.rainbow-ctx.pages.dev

View logs

Copilot AI and others added 2 commits January 20, 2026 22:36
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Copilot AI changed the title [WIP] Add rate limiting to all queries in flashlight Add rate limiting with retry logic to flashlight API queries Jan 20, 2026
Copilot AI requested a review from Amund211 January 20, 2026 22:42
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