Skip to content

feat: add environment variable switch for critical rate limit - #2178

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
LeonDevLifeLog:main
Nov 6, 2025
Merged

feat: add environment variable switch for critical rate limit#2178
seefs001 merged 1 commit into
QuantumNous:mainfrom
LeonDevLifeLog:main

Conversation

@LeonDevLifeLog

@LeonDevLifeLog LeonDevLifeLog commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Added critical rate limiting with configurable enable/disable toggle
    • New environment variables available to customize critical rate limit threshold and time window
    • Default rate limit settings provided for immediate use

@coderabbitai

coderabbitai Bot commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This PR introduces a toggleable critical rate-limiting feature by adding three new configuration variables (enable flag, rate limit number, and duration), initializing them from environment variables with sensible defaults, and making the CriticalRateLimit middleware function conditional on the enable flag.

Changes

Cohort / File(s) Summary
Critical Rate Limit Configuration
common/constants.go
Adds three public variables for critical rate limiting: CriticalRateLimitEnable (bool), CriticalRateLimitNum (int, default 20), and CriticalRateLimitDuration (int64, default 1200). Removes previously duplicated declarations of the latter two, consolidating them into a new rate-limit group with explicit type specification for duration.
Rate Limit Initialization
common/init.go
Initializes the three critical rate-limit variables in InitEnv by reading from environment variables CRITICAL_RATE_LIMIT_ENABLE (default true), CRITICAL_RATE_LIMIT (default 20), and CRITICAL_RATE_LIMIT_DURATION (default 1200). Placed alongside existing rate-limit initialization logic.
Conditional Rate Limit Middleware
middleware/rate-limit.go
Modifies CriticalRateLimit function to conditionally apply rate limiting: returns rateLimitFactory if common.CriticalRateLimitEnable is true, otherwise bypasses rate limiting by returning defNext.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify environment variable names match intended configuration keys and are documented
  • Confirm default values for rate limits (20 requests per 1200 seconds) align with system requirements
  • Check that initialization order and type conversions (int vs. int64) are correct and consistent
  • Validate the conditional logic in rate-limit middleware properly handles both enabled and disabled states
  • Ensure the feature integrates seamlessly with existing rate-limit infrastructure without side effects

Poem

🐰 A critical limit takes its form,
With toggles, counts, and timed reform,
Environment whispers secrets true,
Middleware bends to what's configured—who knew?
Rate limits dance, now under control! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding environment variable controls for the critical rate limit functionality through new configuration variables.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 4243251 and 50e8639.

📒 Files selected for processing (3)
  • common/constants.go (1 hunks)
  • common/init.go (1 hunks)
  • middleware/rate-limit.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
middleware/rate-limit.go (1)
common/constants.go (3)
  • CriticalRateLimitEnable (162-162)
  • CriticalRateLimitNum (163-163)
  • CriticalRateLimitDuration (164-164)
common/init.go (1)
common/constants.go (3)
  • CriticalRateLimitEnable (162-162)
  • CriticalRateLimitNum (163-163)
  • CriticalRateLimitDuration (164-164)
🔇 Additional comments (3)
common/constants.go (1)

162-164: LGTM! Clean addition of critical rate limit configuration.

The new variables follow the established pattern for rate limiting (enable flag, number, duration) and are positioned logically with other rate limit configurations. The default values (20 requests per 20 minutes) are sensible for critical endpoints.

middleware/rate-limit.go (1)

104-109: LGTM! Middleware correctly implements conditional rate limiting.

The implementation mirrors the pattern used in GlobalWebRateLimit() and GlobalAPIRateLimit(), checking the enable flag before applying the rate limiter. The fallback to defNext when disabled is correct.

common/init.go (1)

102-104: LGTM! Proper initialization of critical rate limit configuration.

The environment variable initialization follows the established pattern and uses appropriate defaults. The default of true for CriticalRateLimitEnable maintains backward compatibility by keeping the rate limit active unless explicitly disabled.


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 and usage tips.

@seefs001
seefs001 merged commit 53123aa into QuantumNous:main Nov 6, 2025
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
feat: add environment variable switch for critical rate limit
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