Skip to content

feat(limiter): prune empty keys; fail-safe key_func; docs/tests for headers & proxy#5

Merged
AliYmn merged 6 commits intomasterfrom
f-1
Aug 26, 2025
Merged

feat(limiter): prune empty keys; fail-safe key_func; docs/tests for headers & proxy#5
AliYmn merged 6 commits intomasterfrom
f-1

Conversation

@AliYmn
Copy link
Copy Markdown
Owner

@AliYmn AliYmn commented Aug 26, 2025

Summary by CodeRabbit

  • New Features
    • Added optional rate-limit response headers (X-RateLimit-*, Retry-After).
    • Support for custom keying to rate-limit by user, API key, or path.
    • Proxy-aware mode to respect X-Forwarded-For behind trusted proxies/CDNs.
  • Documentation
    • Expanded README and docs with new configuration options, examples, notes, and updated guidance/roadmap.
  • Tests
    • Added coverage for headers, Retry-After, proxy handling, and custom key functions.
  • Chores
    • Bumped version to 0.1.8.
    • Updated development dependency: pytest-cov version change.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Aug 26, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Extends RateLimiter to support custom keying, proxy-aware IP resolution, and optional rate-limit headers with Retry-After. Updates documentation and examples accordingly, adds tests for new behaviors, bumps package version to 0.1.8, and adjusts a dev dependency version.

Changes

Cohort / File(s) Summary of Changes
Core Limiter Logic
fastapi_throttle/limiter.py
Added key_func, trust_proxy, add_headers options; switched to monotonic timing; updated key resolution (X-Forwarded-For support); modified call to accept Response; emits X-RateLimit-* and Retry-After headers; refined in-memory storage and pruning.
Documentation
README.md, index.rst
Documented new options and usage patterns (custom keying, proxy-aware mode, headers); updated notes, limitations, features, and roadmap; added advanced examples.
Tests
tests/test_limiter.py
Added tests covering headers emission, Retry-After behavior, proxy trust flow, and custom key function per-user limits.
Packaging & Dependencies
setup.py, requirements.txt
Version bump to 0.1.8; changed pytest-cov version from 6.2.1 to 5.0.0.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant ASGI as FastAPI
  participant RL as RateLimiter
  participant Store as In-memory Store
  participant Resp as Response

  Client->>ASGI: HTTP request
  ASGI->>RL: __call__(Request, Response)
  RL->>RL: Resolve key<br/>(key_func | X-Forwarded-For | client.host)
  RL->>RL: Compute window (monotonic)
  RL->>Store: Load/prune timestamps for key
  alt Within limit
    RL->>Store: Append current timestamp
    RL-->>Resp: Optionally set X-RateLimit-* headers
    ASGI-->>Client: 2xx response
  else Exceeds limit
    RL->>RL: Compute Retry-After
    RL-->>Resp: Set Retry-After (and optional headers)
    ASGI-->>Client: 429 Too Many Requests
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

I tap my paw—one, two—then wait,
Count grains of time at the garden gate. ⏳
New keys, trusted trails through proxy air,
Headers whisper, “how many left?”—fair and square.
When bursts abound, I pause—no fret—
Retry-After—then onward, yet! 🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 4d686a1 and 8db015b.

📒 Files selected for processing (6)
  • README.md (6 hunks)
  • fastapi_throttle/limiter.py (3 hunks)
  • index.rst (3 hunks)
  • requirements.txt (1 hunks)
  • setup.py (1 hunks)
  • tests/test_limiter.py (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch f-1

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AliYmn AliYmn merged commit 78b1525 into master Aug 26, 2025
10 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.

1 participant