Skip to content

reject invalid token bucket limits - #19

Open
HrachShah wants to merge 3 commits into
mainfrom
patch/reject-invalid-bucket-settings
Open

reject invalid token bucket limits#19
HrachShah wants to merge 3 commits into
mainfrom
patch/reject-invalid-bucket-settings

Conversation

@HrachShah

@HrachShah HrachShah commented Jul 30, 2026

Copy link
Copy Markdown
Owner

A zero or negative request rate creates a bucket that can never refill, while a non-positive burst capacity creates an unusable token bucket. Both values are now rejected when middleware starts, with clear ValueError messages. Verification: Python compilation and direct constructor assertions.

Summary by Sourcery

Bug Fixes:

  • Prevent creation of token buckets with non-positive request rates or burst capacities that would never refill or be unusable.

Summary by CodeRabbit

  • Bug Fixes
    • Added validation to prevent invalid rate-limit settings.
    • Clear errors are now shown when request or burst limits are set below 1.
    • Rate limits now refill and expire reliably, even when system time changes.
    • Concurrent requests are handled atomically, preventing requests from exceeding configured limits.

@sourcery-ai

sourcery-ai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR hardens the rate limiting middleware by validating token bucket configuration at initialization, rejecting non-positive request rates and burst capacities with clear ValueError messages before delegating to the base middleware setup.

Flow diagram for rate_limit middleware initialization validation

flowchart TD
    A["RateLimitMiddleware.__init__(app, requests_per_minute, burst_capacity)"] --> B{"requests_per_minute < 1"}
    B -- Yes --> C["raise ValueError requests_per_minute must be at least 1"]
    B -- No --> D{"burst_capacity < 1"}
    D -- Yes --> E["raise ValueError burst_capacity must be at least 1"]
    D -- No --> F["super().__init__(app)"]
    F --> G["set self.requests_per_minute"]
    G --> H["set self.burst_capacity"]
Loading

File-Level Changes

Change Details Files
Validate token bucket configuration to reject unusable rate limit settings at middleware initialization time.
  • Add a guard that raises ValueError when requests_per_minute is less than 1
  • Add a guard that raises ValueError when burst_capacity is less than 1
  • Ensure validation runs before calling the superclass initializer and assigning configuration fields
freerelay/middleware/rate_limit.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 05ae4dde-4531-4dc1-acc7-ccfb3832fd1c

📥 Commits

Reviewing files that changed from the base of the PR and between 0fdaa36 and 2dcb5a3.

📒 Files selected for processing (2)
  • freerelay/middleware/rate_limit.py
  • tests/unit/test_rate_limit.py

📝 Walkthrough

Walkthrough

TokenBucket and RateLimitMiddleware now validate positive limits, use monotonic timestamps, and serialize shared bucket operations with an asyncio.Lock. Tests cover timing, validation, and concurrent requests.

Changes

Rate limit controls

Layer / File(s) Summary
Token bucket validation and timing
freerelay/middleware/rate_limit.py, tests/unit/test_rate_limit.py
TokenBucket rejects non-positive rate and capacity values. Refill calculations use time.monotonic(). Tests verify validation and refill behavior.
Middleware state validation and locking
freerelay/middleware/rate_limit.py
RateLimitMiddleware rejects request or burst limits below one. Cleanup and bucket operations run under an asyncio.Lock and use monotonic timestamps.
Concurrency validation
tests/unit/test_rate_limit.py
Concurrent requests sharing one bucket produce one HTTP 429 response and one downstream call.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant RateLimitMiddleware
  participant StateLock
  participant TokenBucket
  participant Downstream
  Client->>RateLimitMiddleware: send concurrent requests
  RateLimitMiddleware->>StateLock: serialize bucket access
  StateLock->>TokenBucket: consume token
  TokenBucket-->>RateLimitMiddleware: allow or reject
  RateLimitMiddleware->>Downstream: forward allowed request
  RateLimitMiddleware-->>Client: response or HTTP 429
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: rejecting invalid token bucket limits.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch patch/reject-invalid-bucket-settings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant