-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-11739] Clean up rate limiting area, decouple GlobalConfig in APISpec #6262
[TT-11739] Clean up rate limiting area, decouple GlobalConfig in APISpec #6262
Conversation
API Changes --- prev.txt 2024-05-16 14:17:40.992509241 +0000
+++ current.txt 2024-05-16 14:17:36.968433493 +0000
@@ -5815,7 +5815,7 @@
// EnableFixedWindow enables fixed window rate limiting.
EnableFixedWindowRateLimiter bool `json:"enable_fixed_window_rate_limiter"`
- // Redis based rate limiter with fixed window. Provides 100% rate limiting accuracy, but require two additional Redis roundtrip for each request.
+ // Redis based rate limiter with sliding log. Provides 100% rate limiting accuracy, but require two additional Redis roundtrip for each request.
EnableRedisRollingLimiter bool `json:"enable_redis_rolling_limiter"`
// To enable, set to `true`. The sentinel-based rate limiter delivers a smoother performance curve as rate-limit calculations happen off-thread, but a stricter time-out based cool-down for clients. For example, when a throttling action is triggered, they are required to cool-down for the period of the rate limit.
@@ -7090,8 +7090,11 @@
const (
// QuotaKeyPrefix serves as a standard prefix for generating quota keys.
QuotaKeyPrefix = "quota-"
- // RateLimitKeyPrefix serves as a standard prefix for generating rate limit keys.
- RateLimitKeyPrefix = "rate-limit-"
+
+ // RateLimitKeyPrefix serves as a standard prefix for generating rate limiter keys.
+ RateLimitKeyPrefix = rate.LimiterKeyPrefix
+
+ // SentinelRateLimitKeyPostfix is appended to the rate limiting key to combine into a sentinel key.
SentinelRateLimitKeyPostfix = ".BLOCKED"
)
const (
@@ -9690,7 +9693,7 @@
func (l *SessionLimiter) Context() context.Context
-func (l *SessionLimiter) ForwardMessage(r *http.Request, currentSession *user.SessionState, rateLimitKey string, quotaKey string, store storage.Handler, enableRL, enableQ bool, globalConf *config.Config, api *APISpec, dryRun bool) sessionFailReason
+func (l *SessionLimiter) ForwardMessage(r *http.Request, currentSession *user.SessionState, rateLimitKey string, quotaKey string, store storage.Handler, enableRL, enableQ bool, api *APISpec, dryRun bool) sessionFailReason
ForwardMessage will enforce rate limiting, returning a non-zero
sessionFailReason if session limits have been exceeded. Key values to manage
rate are Rate and Per, e.g. Rate of 10 messages Per 10 seconds |
PR Description updated to latest commit (c130a1b) |
PR Review 🔍
Code feedback:
|
c130a1b
to
3b368cb
Compare
PR Code Suggestions ✨
|
3b368cb
to
ca5cb60
Compare
💥 CI tests failed 🙈git-stateall ok Please look at the run or in the Checks tab. |
4e5170c
to
8817cb9
Compare
💥 CI tests failed 🙈git-stateall ok Please look at the run or in the Checks tab. |
Please retry analysis of this Pull-Request directly on SonarCloud |
💥 CI tests failed 🙈git-stateall ok Please look at the run or in the Checks tab. |
62bf375
to
a12a2a6
Compare
Quality Gate failedFailed conditions See analysis details on SonarCloud Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
…pec (#6262) ### **User description** Clean up refactor of rate limits area: - use rate limiter config object (apispec.GlobalConfig dropped) - moved rate limiter key computation to happen before rate limiter logic, deduplicate - minimized passed arguments Fixing bugs: - fixes new rate limiter not proceeding to quota to maintain existing behaviour - key computation on new rate limiter now consistent with others (`rate-` prefix) https://tyktech.atlassian.net/browse/TT-11739 ___ ### **PR Type** Bug fix, Enhancement ___ ### **Description** - Decoupled `GlobalConfig` from various rate limiting and quota management functions across multiple files to enhance modularity. - Centralized rate limiter key prefix usage and standardized key generation logic. - Removed redundant parameters and streamlined logic in session management and rate limiting functions. - Added new utility functions to manage rate limiter configurations and key generation more effectively. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>mw_api_rate_limit.go</strong><dd><code>Simplify API Rate Limiting Logic and Decouple Global Config</code></dd></summary> <hr> gateway/mw_api_rate_limit.go <li>Removed reference to <code>GlobalConfig</code> from rate limiting logic.<br> <li> Simplified function arguments by removing unnecessary parameters.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-46326b04f936c839922e970db5c2924156cc797070948f3dc9c589d04661d6d2">+0/-1</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_organisation_activity.go</strong><dd><code>Refactor Organisation Activity Rate Limiting</code> </dd></summary> <hr> gateway/mw_organisation_activity.go <li>Removed <code>GlobalConfig</code> references in rate limiting checks.<br> <li> Streamlined function parameters to enhance clarity and <br>maintainability.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-26dd955903317b085be06642ae3e76fe41c8c53844d8758a1a1c8bd05b0110a2">+0/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_rate_limiting.go</strong><dd><code>Refactor Rate Limiting and Quota Checks</code> </dd></summary> <hr> gateway/mw_rate_limiting.go <li>Decoupled <code>GlobalConfig</code> from rate limiting and quota checks.<br> <li> Consolidated rate limiting key computation logic.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-4bf8ae01ccab67bb786468f793f6bb4324c8f6b950b0e98e203effebe763a630">+0/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>session_manager.go</strong><dd><code>Centralize Rate Limiter Key Prefix and Optimize Session Limiter</code></dd></summary> <hr> gateway/session_manager.go <li>Centralized rate limiter key prefix to <code>rate.LimiterKeyPrefix</code>.<br> <li> Optimized session limiter functions by removing redundant parameters <br>and simplifying logic.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-e6b40a285464cd86736e970c4c0b320b44c75b18b363d38c200e9a9d36cdabb6">+42/-74</a> </td> </tr> <tr> <td> <details> <summary><strong>limiter.go</strong><dd><code>Enhance Rate Limiter Configuration and Key Generation</code> </dd></summary> <hr> internal/rate/limiter.go <li>Added <code>LimiterKey</code> function to standardize rate limiter key generation.<br> <li> Enhanced modularity by separating limiter configuration logic.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-84e3fda0965028c6e464ae0916aaef0af63d0367fde6f910b513a610f2a34ee5">+16/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>rate.go</strong><dd><code>Update Constants and Error Handling for Rate Limiters</code> </dd></summary> <hr> internal/rate/rate.go <li>Introduced <code>LimiterKeyPrefix</code> for consistent key naming.<br> <li> Updated constants and error handling for rate limiters.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6262/files#diff-666e0b0c32c831ededfd6c780dbf916d10e4b7d9c8e02f157bf1dca1d58a360d">+10/-3</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions --------- Co-authored-by: Tit Petric <tit@tyk.io>
User description
Clean up refactor of rate limits area:
Fixing bugs:
rate-
prefix)https://tyktech.atlassian.net/browse/TT-11739
PR Type
Bug fix, Enhancement
Description
GlobalConfig
from various rate limiting and quota management functions across multiple files to enhance modularity.Changes walkthrough 📝
mw_api_rate_limit.go
Simplify API Rate Limiting Logic and Decouple Global Config
gateway/mw_api_rate_limit.go
GlobalConfig
from rate limiting logic.mw_organisation_activity.go
Refactor Organisation Activity Rate Limiting
gateway/mw_organisation_activity.go
GlobalConfig
references in rate limiting checks.maintainability.
mw_rate_limiting.go
Refactor Rate Limiting and Quota Checks
gateway/mw_rate_limiting.go
GlobalConfig
from rate limiting and quota checks.session_manager.go
Centralize Rate Limiter Key Prefix and Optimize Session Limiter
gateway/session_manager.go
rate.LimiterKeyPrefix
.and simplifying logic.
limiter.go
Enhance Rate Limiter Configuration and Key Generation
internal/rate/limiter.go
LimiterKey
function to standardize rate limiter key generation.rate.go
Update Constants and Error Handling for Rate Limiters
internal/rate/rate.go
LimiterKeyPrefix
for consistent key naming.