Skip to content

wip: redis based ratelimiting#3075

Merged
chronark merged 3 commits intomainfrom
redis-ratelimiting
Apr 4, 2025
Merged

wip: redis based ratelimiting#3075
chronark merged 3 commits intomainfrom
redis-ratelimiting

Conversation

@chronark
Copy link
Collaborator

@chronark chronark commented Apr 3, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a distributed counter-based rate limiting mechanism with standardized reset time responses.
    • Added new configuration options including an instance identifier, a Redis connection URL, and a Test Mode flag for simplified setup and testing.
    • Enhanced monitoring with an integrated Prometheus metrics endpoint.
  • Refactor

    • Streamlined API operation by removing legacy cluster-based configurations.
    • Improved testing scenarios and error handling for increased robustness.
  • Documentation

    • Released updated guidance for the distributed counter functionality.

@vercel
Copy link

vercel bot commented Apr 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 11:46am
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 11:46am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 11:46am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
dashboard ⬜️ Ignored (Inspect) Visit Preview Apr 4, 2025 11:46am

@changeset-bot
Copy link

changeset-bot bot commented Apr 3, 2025

⚠️ No Changeset found

Latest commit: 34c7310

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2025

Hey there and thank you for opening this pull request! 👋🏼

We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted.
Here is an example:

<type>[optional scope]: <description>
fix: I fixed something for Unkey

Details:

Unknown release type "wip" found in pull request title "wip: redis based ratelimiting". 

Available types:
 - feat: A new feature
 - fix: A bug fix
 - docs: Documentation only changes
 - style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
 - refactor: A code change that neither fixes a bug nor adds a feature
 - perf: A code change that improves performance
 - test: Adding missing tests or correcting existing tests
 - build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
 - ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
 - chore: Other changes that don't modify src or test files
 - revert: Reverts a previous commit

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 3, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This pull request removes legacy cluster-based functionality and replaces it with a simpler distributed counter approach backed by Redis. The API configuration and CLI flags now use an instance identifier, Redis URL, and a new test mode flag. Internal rate limiting modules have been refactored to use local window structures and counter updates instead of peer synchronization. Test suites have been updated accordingly, and related service discovery, RPC, and cluster tests have been removed. In addition, documentation and error logging have been enhanced in the new counter and Prometheus packages.

Changes

Files/Paths Summary
go/Taskfile.yml Updated the test-full task command by adding the -v flag for verbose test output.
go/apps/api/{config.go, cancel_test.go, integration/multi_node_ratelimiting/accuracy_test.go, routes/{register.go, v2_ratelimit_limit/accuracy_test.go, v2_ratelimit_limit/handler.go}, run.go, cmd/api/main.go, run_test.go} Removed cluster configurations and replaced them with new fields (InstanceID, RedisUrl, TestMode). Updated test logic (e.g., renaming tests, removing parallel execution) and CLI flags; deleted legacy cluster tests.
go/internal/services/ratelimit/{bucket.go, interface.go, janitor.go, peers.go, replay.go, service.go, util.go, window.go} Eliminated external ratelimitv1 types and cluster/peer management. Introduced a local window struct, simplified time comparisons, removed replay logic, and replaced cluster synchronization with a counter-based mechanism.
go/pkg/counter/{doc.go, interface.go, redis.go, redis_test.go} Introduced a new counter package with comprehensive documentation, a Counter interface, a Redis-backed implementation, and an accompanying test suite.
go/pkg/prometheus/{promhttp.go, servicediscovery.go} Refactored the Prometheus metrics handler by removing cluster and RPC dependencies; obsolete service discovery files have been deleted.
go/pkg/testutil/containers/{api.go, redis.go}, go/pkg/testutil/http.go Updated Docker container configurations: renamed Redis variable, reduced exposed ports, removed clustering environment variables, added test mode variable, and integrated the new counter service into the HTTP test harness.
go/pkg/zen/{middleware_errors.go, server.go} Enhanced error logging by including a request ID and added a new Flags struct (with a TestMode flag) to the server configuration.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant AS as API Server
    participant RS as Redis Counter Service
    C->>AS: Send API request (X-Test-Time header if TestMode)
    AS->>RS: Increment counter and/or get current counter state
    RS-->>AS: Return counter value and reset time
    AS->>C: Respond with rate limit decision and reset time
Loading

Suggested reviewers

  • perkinsjr

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c9acd76 and 34c7310.

⛔ Files ignored due to path filters (2)
  • go/gen/proto/ratelimit/v1/ratelimitv1connect/service.connect.go is excluded by !**/gen/**
  • go/gen/proto/ratelimit/v1/service.pb.go is excluded by !**/*.pb.go, !**/gen/**
📒 Files selected for processing (29)
  • go/Taskfile.yml (1 hunks)
  • go/apps/api/cancel_test.go (2 hunks)
  • go/apps/api/config.go (3 hunks)
  • go/apps/api/integration/multi_node_ratelimiting/accuracy_test.go (1 hunks)
  • go/apps/api/routes/register.go (1 hunks)
  • go/apps/api/routes/v2_ratelimit_limit/accuracy_test.go (0 hunks)
  • go/apps/api/routes/v2_ratelimit_limit/handler.go (4 hunks)
  • go/apps/api/run.go (7 hunks)
  • go/apps/api/run_test.go (0 hunks)
  • go/cmd/api/main.go (3 hunks)
  • go/internal/services/ratelimit/bucket.go (5 hunks)
  • go/internal/services/ratelimit/interface.go (2 hunks)
  • go/internal/services/ratelimit/janitor.go (1 hunks)
  • go/internal/services/ratelimit/peers.go (0 hunks)
  • go/internal/services/ratelimit/replay.go (4 hunks)
  • go/internal/services/ratelimit/service.go (6 hunks)
  • go/internal/services/ratelimit/util.go (1 hunks)
  • go/internal/services/ratelimit/window.go (2 hunks)
  • go/pkg/counter/doc.go (1 hunks)
  • go/pkg/counter/interface.go (1 hunks)
  • go/pkg/counter/redis.go (1 hunks)
  • go/pkg/counter/redis_test.go (1 hunks)
  • go/pkg/prometheus/promhttp.go (5 hunks)
  • go/pkg/prometheus/servicediscovery.go (0 hunks)
  • go/pkg/testutil/containers/api.go (2 hunks)
  • go/pkg/testutil/containers/redis.go (1 hunks)
  • go/pkg/testutil/http.go (4 hunks)
  • go/pkg/zen/middleware_errors.go (1 hunks)
  • go/pkg/zen/server.go (3 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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.

@chronark chronark marked this pull request as ready for review April 4, 2025 11:50
@chronark chronark merged commit 6af81e0 into main Apr 4, 2025
34 of 36 checks passed
@chronark chronark deleted the redis-ratelimiting branch April 4, 2025 11:50
@coderabbitai coderabbitai bot mentioned this pull request Jul 2, 2025
13 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jul 21, 2025
18 tasks
@coderabbitai coderabbitai bot mentioned this pull request Aug 15, 2025
18 tasks
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