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-11927] [poc]implement ratelimit smoothing #6250
Draft
jeffy-mathew
wants to merge
1
commit into
master
Choose a base branch
from
poc/TT-11927/rate-limit-smoothing
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+140
−15
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API Changes --- prev.txt 2024-04-27 21:49:11.072140126 +0000
+++ current.txt 2024-04-27 21:49:08.180140924 +0000
@@ -5621,6 +5621,9 @@
// Controls which algorthm to use as a fallback when your distributed rate limiter can't be used.
DRLEnableSentinelRateLimiter bool `json:"drl_enable_sentinel_rate_limiter"`
+
+ // EnableRateLimitSmoothing enables rate limit smoothing.
+ EnableRateLimitSmoothing bool `json:"enable_rate_limit_smoothing"`
}
RateLimit contains flags and configuration for controlling rate limiting
behaviour. It is embedded in the main config structure.
@@ -6788,6 +6791,7 @@
EventTokenCreated apidef.TykEvent = "TokenCreated"
EventTokenUpdated apidef.TykEvent = "TokenUpdated"
EventTokenDeleted apidef.TykEvent = "TokenDeleted"
+ EventRateLimitingSmoothed apidef.TykEvent = "RateLimitSmoothing"
)
Register new event types here, the string is the code used to hook at the
Api Deifnititon JSON/BSON level
@@ -8424,6 +8428,8 @@
func (l *LDAPStorageHandler) LoadConfFromMeta(meta map[string]interface{})
+func (l LDAPStorageHandler) Lock(string, time.Duration) (bool, error)
+
func (l LDAPStorageHandler) RemoveFromList(keyName, value string) error
func (l LDAPStorageHandler) RemoveFromSet(keyName, value string)
@@ -8881,6 +8887,8 @@
func (r RPCStorageHandler) IsRetriableError(err error) bool
+func (r *RPCStorageHandler) Lock(key string, timeout time.Duration) (bool, error)
+
func (r *RPCStorageHandler) ProcessKeySpaceChanges(keys []string, orgId string)
ProcessKeySpaceChanges receives an array of keys to be processed,
those keys are considered changes in the keyspace in the management layer,
@@ -9450,12 +9458,15 @@
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,
+ globalConf *config.Config, 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
-func (l *SessionLimiter) RedisQuotaExceeded(r *http.Request, currentSession *user.SessionState, quotaKey, scope string, limit *user.APILimit, store storage.Handler, hashKeys bool) bool
+func (l *SessionLimiter) RedisQuotaExceeded(r *http.Request, currentSession *user.SessionState, quotaKey, scope string,
+ limit *user.APILimit, store storage.Handler, hashKeys bool) bool
type SlaveDataCenter struct {
SlaveOptions config.SlaveOptionsConfig
@@ -10803,6 +10814,7 @@
RemoveFromList(string, string) error
AppendToSet(string, string)
Exists(string) (bool, error)
+ Lock(string, time.Duration) (bool, error)
}
Handler is a standard interface to a storage backend, used by
AuthorisationManager to read and write key values to the backend
@@ -10865,6 +10877,8 @@
func (m MdcbStorage) IncrememntWithExpire(string, int64) int64
+func (m MdcbStorage) Lock(key string, timeout time.Duration) (bool, error)
+
func (m MdcbStorage) RemoveFromList(key string, value string) error
func (m MdcbStorage) RemoveFromSet(key string, value string)
@@ -11611,6 +11625,9 @@
QuotaRemaining int64 `json:"quota_remaining" msg:"quota_remaining"`
QuotaRenewalRate int64 `json:"quota_renewal_rate" msg:"quota_renewal_rate"`
SetBy string `json:"-" msg:"-"`
+
+ // smoothing related configuration
+ Smoothing *RateLimitSmoothing `json:"smoothing,omitempty" msg:"smoothing,omitempty"`
}
APILimit stores quota and rate limit on ACL level (per API)
@@ -11702,6 +11719,14 @@
PerAPI bool `bson:"per_api" json:"per_api"`
}
+type RateLimitSmoothing struct {
+ Rate int64 `json:"rate"`
+ Interval int64 `json:"interval"`
+ Threshold int64 `json:"threshold"`
+ Trigger float64 `json:"trigger"`
+ CurrentAllowance int64 `json:"currentAllowance"`
+}
+
type SessionState struct {
LastCheck int64 `json:"last_check" msg:"last_check"`
Allowance float64 `json:"allowance" msg:"allowance"`
@@ -11744,6 +11769,9 @@
SessionLifetime int64 `bson:"session_lifetime" json:"session_lifetime"`
KeyID string `json:"-"`
+
+ // smoothing related configuration
+ Smoothing *RateLimitSmoothing `json:"smoothing,omitempty" msg:"smoothing,omitempty"`
// Has unexported fields.
}
SessionState objects represent a current API session, mainly used |
jeffy-mathew
changed the title
[TT-11927] implement ratelimit smoothing
[TT-11927] [poc]implement ratelimit smoothing
Apr 26, 2024
jeffy-mathew
force-pushed
the
poc/TT-11927/rate-limit-smoothing
branch
from
April 27, 2024 21:48
e8a9703
to
15e4911
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Related Issue
Motivation and Context
How This Has Been Tested
Screenshots (if appropriate)
Types of changes
Checklist