@@ -50,12 +50,13 @@ const (
5050// SessionLimiter is the rate limiter for the API, use ForwardMessage() to
5151// check if a message should pass through or not
5252type SessionLimiter struct {
53- ctx context.Context
54- drlManager * drl.DRL
55- config * config.Config
56- bucketStore model.BucketStorage
57- limiterStorage redis.UniversalClient
58- smoothing * rate.Smoothing
53+ ctx context.Context
54+ drlManager * drl.DRL
55+ config * config.Config
56+ bucketStore model.BucketStorage
57+ limiterStorage redis.UniversalClient
58+ smoothing * rate.Smoothing
59+
5960 enableContextVariables bool
6061}
6162
@@ -68,10 +69,11 @@ type SessionLimiter struct {
6869// back onto the default gateway storage configuration.
6970func NewSessionLimiter (ctx context.Context , conf * config.Config , drlManager * drl.DRL , externalServicesConfig * config.ExternalServiceConfig ) SessionLimiter {
7071 sessionLimiter := SessionLimiter {
71- ctx : ctx ,
72- drlManager : drlManager ,
73- config : conf ,
74- bucketStore : memorycache .New (ctx ),
72+ ctx : ctx ,
73+ drlManager : drlManager ,
74+ config : conf ,
75+ bucketStore : memorycache .New (ctx ),
76+
7577 enableContextVariables : conf .EnableContextVariables ,
7678 }
7779
@@ -93,14 +95,7 @@ func (l *SessionLimiter) Context() context.Context {
9395 return l .ctx
9496}
9597
96- func (l * SessionLimiter ) doRollingWindowWrite (
97- r * http.Request ,
98- session * user.SessionState ,
99- rateLimiterKey string ,
100- apiLimit * user.APILimit ,
101- dryRun bool ,
102- ) (time.Duration , bool ) {
103-
98+ func (l * SessionLimiter ) doRollingWindowWrite (r * http.Request , session * user.SessionState , rateLimiterKey string , apiLimit * user.APILimit , dryRun bool ) (time.Duration , bool ) {
10499 ctx := l .Context ()
105100 rateLimiterSentinelKey := rateLimiterKey + SentinelRateLimitKeyPostfix
106101
@@ -170,14 +165,7 @@ func (l *SessionLimiter) doRollingWindowWrite(
170165 return blockDuration , shouldBlock
171166}
172167
173- func (l * SessionLimiter ) limitSentinel (
174- r * http.Request ,
175- session * user.SessionState ,
176- rateLimiterKey string ,
177- apiLimit * user.APILimit ,
178- dryRun bool ,
179- ) (time.Duration , bool ) {
180-
168+ func (l * SessionLimiter ) limitSentinel (r * http.Request , session * user.SessionState , rateLimiterKey string , apiLimit * user.APILimit , dryRun bool ) (time.Duration , bool ) {
181169 defer func () {
182170 go l .doRollingWindowWrite (r , session , rateLimiterKey , apiLimit , dryRun )
183171 }()
@@ -191,14 +179,7 @@ func (l *SessionLimiter) limitSentinel(
191179 return expires , err == nil
192180}
193181
194- func (l * SessionLimiter ) limitRedis (
195- r * http.Request ,
196- session * user.SessionState ,
197- rateLimiterKey string ,
198- apiLimit * user.APILimit ,
199- dryRun bool ,
200- ) (time.Duration , bool ) {
201-
182+ func (l * SessionLimiter ) limitRedis (r * http.Request , session * user.SessionState , rateLimiterKey string , apiLimit * user.APILimit , dryRun bool ) (time.Duration , bool ) {
202183 return l .doRollingWindowWrite (r , session , rateLimiterKey , apiLimit , dryRun )
203184}
204185
@@ -276,7 +257,7 @@ func (l *SessionLimiter) RateLimitInfo(r *http.Request, api *APISpec, endpoints
276257}
277258
278259// ForwardMessage will enforce rate limiting, returning a non-zero
279- // sessionFailReason if session limits have been exceeded.
260+ // sessionFailReasonMarker if session limits have been exceeded.
280261// Key values to manage rate are Rate and Per, e.g. Rate of 10 messages
281262// Per 10 seconds
282263func (l * SessionLimiter ) ForwardMessage (
@@ -296,16 +277,7 @@ func (l *SessionLimiter) ForwardMessage(
296277 )
297278}
298279
299- func (l * SessionLimiter ) forwardMessageInternal (
300- r * http.Request ,
301- session * user.SessionState ,
302- rateLimitKey string ,
303- quotaKey string ,
304- store storage.Handler ,
305- enableRL , enableQ bool ,
306- api * APISpec ,
307- dryRun bool ,
308- ) sessionFailReason {
280+ func (l * SessionLimiter ) forwardMessageInternal (r * http.Request , session * user.SessionState , rateLimitKey string , quotaKey string , store storage.Handler , enableRL , enableQ bool , api * APISpec , dryRun bool ) sessionFailReason {
309281 // check for limit on API level (set to session by ApplyPolicies)
310282 accessDef , allowanceScope , err := GetAccessDefinitionByAPIIDOrSession (session , api )
311283 if err != nil {
@@ -492,7 +464,6 @@ func (l *SessionLimiter) RedisQuotaExceeded(r *http.Request, session *user.Sessi
492464
493465 l .updateSessionQuota (session , scope , remaining , expiredAt .Unix ())
494466 l .extendContextWithQuota (r , int (limit .QuotaMax ), int (remaining ), int (expiredAt .Unix ()))
495-
496467 return blocked
497468 }
498469
@@ -580,68 +551,49 @@ func (*SessionLimiter) updateSessionQuota(session *user.SessionState, scope stri
580551 session .Touch ()
581552}
582553
583- func (l * SessionLimiter ) extendContext (
584- r * http.Request ,
585- result sessionFailReason ,
586- ) sessionFailReason {
587-
554+ func (l * SessionLimiter ) extendContext (r * http.Request , result sessionFailReason ) sessionFailReason {
588555 if failRateLimit , ok := result .(sessionFailRateLimit ); ok {
589556 l .extendContextWithLimits (r , failRateLimit )
590557 }
591-
592558 return result
593559}
594560
595- func (l * SessionLimiter ) extendContextWithQuota (
596- r * http.Request ,
597- quotaMax , remaining , reset int ,
598- ) {
599-
561+ func (l * SessionLimiter ) extendContextWithQuota (r * http.Request , quotaMax , remaining , reset int ) {
600562 if ! l .enableContextVariables {
601563 return
602564 }
603-
604565 data := ctxGetOrCreateData (r )
605-
606566 data [ctxDataKeyQuotaLimit ] = quotaMax
607567 data [ctxDataKeyQuotaRemaining ] = remaining
608568 data [ctxDataKeyQuotaReset ] = reset
609569}
610570
611- func (l * SessionLimiter ) extendContextWithLimits (
612- r * http.Request ,
613- frl sessionFailRateLimit ,
614- ) {
615-
571+ func (l * SessionLimiter ) extendContextWithLimits (r * http.Request , frl sessionFailRateLimit ) {
616572 if ! l .enableContextVariables {
617573 return
618574 }
619-
620575 data := ctxGetOrCreateData (r )
621-
622576 data [ctxDataKeyRateLimitLimit ] = int (frl .limit )
623577 data [ctxDataKeyRateLimitRemaining ] = 0
624578 data [ctxDataKeyRateLimitReset ] = int (frl .reset .Seconds ())
625579}
626580
627581type sessionFailReason interface {
628- // sessionFailReason marker method
629- sessionFailReason ()
582+ sessionFailReasonMarker ()
630583}
631584
632585type sessionFailNone struct {}
633586type sessionFailRateLimit struct {
634- reset time.Duration
635- limit uint
636- per uint
587+ reset time.Duration
588+ limit , per uint
637589}
638590type sessionFailQuota struct {}
639591type sessionFailInternalServerError struct {}
640592
641- func (sessionFailNone ) sessionFailReason () {}
642- func (sessionFailRateLimit ) sessionFailReason () {}
643- func (sessionFailQuota ) sessionFailReason () {}
644- func (sessionFailInternalServerError ) sessionFailReason () {}
593+ func (sessionFailNone ) sessionFailReasonMarker () {}
594+ func (sessionFailRateLimit ) sessionFailReasonMarker () {}
595+ func (sessionFailQuota ) sessionFailReasonMarker () {}
596+ func (sessionFailInternalServerError ) sessionFailReasonMarker () {}
645597
646598func newSessionFailRateLimit (limits * user.APILimit , ttl time.Duration ) sessionFailRateLimit {
647599 return sessionFailRateLimit {
0 commit comments