TT-6075: Update rate limit implementation#7941
TT-6075: Update rate limit implementation#7941vladzabolotnyi wants to merge 76 commits intomasterfrom
Conversation
|
This pull request introduces a significant refactoring of the rate limit header implementation. It decouples the source of rate limit headers from the quota system, allowing for more accurate, real-time feedback based directly on the rate-limiting mechanism. A new configuration option, Files Changed AnalysisThe changes span 30 files, with 1332 additions and 188 deletions, reflecting a substantial overhaul of the rate-limiting and response header logic.
Architecture & Impact Assessment
Flow DiagramsequenceDiagram
participant Client
participant Gateway as Tyk Gateway
participant SessionLimiter
participant HeaderSender
participant Upstream
Client->>Gateway: Request
Gateway->>SessionLimiter: ForwardMessage(req, session, headerSender)
activate SessionLimiter
SessionLimiter->>SessionLimiter: newRateLimitChecker().Check()
note right of SessionLimiter: Determines if request is over limit and gets stats
SessionLimiter->>HeaderSender: SendRateLimits(stats)
activate HeaderSender
note right of HeaderSender: In 'rate_limits' mode, sets X-RateLimit-* headers
HeaderSender-->>Gateway: Headers are now on the response writer
deactivate HeaderSender
alt Rate Limit Exceeded
SessionLimiter-->>Gateway: return sessionFailRateLimit
Gateway-->>Client: 429 Too Many Requests (with headers)
else Rate Limit OK
SessionLimiter-->>Gateway: return sessionFailNone
deactivate SessionLimiter
Gateway->>Upstream: Forward Request
Upstream-->>Gateway: Upstream Response
Gateway->>HeaderSender: SendQuotas(session)
note right of HeaderSender: In 'quotas' mode, sets headers here (legacy behavior)
Gateway-->>Client: 200 OK (with final headers)
end
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-04-20T09:10:44.022Z | Triggered by: pr_updated | Commit: 93571d1 💡 TIP: You can chat with Visor using |
Security Issues (2)
Performance Issues (2)
Security Issues (2)
Performance Issues (2)
Powered by Visor from Probelabs Last updated: 2026-04-20T09:10:17.575Z | Triggered by: pr_updated | Commit: 93571d1 💡 TIP: You can chat with Visor using |
…6075/update-rate-limit-header-logic
…nstead raw requests count
…ith returning positive values when should block
|
All sonarqube issues are related to constant initialization at test file with avoiding value duplication and cognitive complexity in legacy logic. This changes can be fixed but not in the scope of current ticket. |
…nd update the marshalled field
Co-authored-by: andyo-tyk <99968932+andyo-tyk@users.noreply.github.com>
andyo-tyk
left a comment
There was a problem hiding this comment.
Go docs LGTM. This does not cover developer/engineer code review.
|
🚨 Jira Linter FailedCommit: The Jira linter failed to validate your PR. Please check the error details below: 🔍 Click to view error detailsNext Steps
This comment will be automatically deleted once the linter passes. |



Description
Update logic with handling rate limit headers data. Before we always set the data from quota. Now we're adding configuration to select the source for headers value.
Motivation and Context
How This Has Been Tested
Demos
Given API with 3/15s RL and 5 Quotas && "ratelimit_headers_source": "quotas", "enable_redis_rolling_limiter": true
Screen.Recording.2026-04-02.at.16.49.37.mov
When client sends 3 request, Headers are present and reflect Quota state(3/5). After 3 requests the RL is hit and 429 code returned. Wait 15 secs, send 2 more requests to return 403 and headers are omitted. We don't include headers on error response to support backward compatibility. When rate limit is hit we can see 429 status code and for quotas limit we could see 403, that's why we have different status codes - rate limit is recovering but quotas has much longer delay to recover
Screen.Recording.2026-04-02.at.17.14.18.mov
Given API with 3/5s RL and 5 Quotas && "ratelimit_headers_source": "rate_limits", "enable_redis_rolling_limiter": true. Send 3 requests, hit 429 code, headers must be present reflecting current state. Wait till RL refresh, send 2 more requests to hit the quotas limit and 403 status code. Headers are present reflecting rate limit state.
Screen.Recording.2026-04-02.at.17.32.33.mov
Context variables attached to response headers
Screen.Recording.2026-04-03.at.13.22.29.mov
Types of changes
Checklist