@@ -77,10 +77,10 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
7777 e .currHits ++
7878
7979 // Calculate when it resets in seconds
80- expire := e .exp - ts
80+ resetInSec := e .exp - ts
8181
8282 // weight = time until current window reset / total window length
83- weight := float64 (expire ) / float64 (expiration )
83+ weight := float64 (resetInSec ) / float64 (expiration )
8484
8585 // rate = request count in previous window - weight + request count in current window
8686 rate := int (float64 (e .prevHits )* weight ) + e .currHits
@@ -89,18 +89,18 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
8989 remaining := cfg .Max - rate
9090
9191 // Update storage. Garbage collect when the next window ends.
92- // |-------------------------| -------------------------|
93- // ^ ^ ^ ^
94- // ts e.exp End sample window End next window
95- // <----------->
96- // expire
97- // expire = e.exp - ts - time until end of current window.
92+ // |--------------------------|- -------------------------|
93+ // ^ ^ ^ ^
94+ // ts e.exp End sample window End next window
95+ // <------------ >
96+ // resetInSec
97+ // resetInSec = e.exp - ts - time until end of current window.
9898 // duration + expiration = end of next window.
9999 // Because we don't want to garbage collect in the middle of a window
100100 // we add the expiration to the duration.
101101 // Otherwise after the end of "sample window", attackers could launch
102102 // a new request with the full window length.
103- manager .set (key , e , time .Duration (expire + expiration )* time .Second )
103+ manager .set (key , e , time .Duration (resetInSec + expiration )* time .Second )
104104
105105 // Unlock entry
106106 mux .Unlock ()
@@ -109,7 +109,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
109109 if remaining < 0 {
110110 // Return response with Retry-After header
111111 // https://tools.ietf.org/html/rfc6584
112- c .Set (fiber .HeaderRetryAfter , strconv .FormatUint (expire , 10 ))
112+ c .Set (fiber .HeaderRetryAfter , strconv .FormatUint (resetInSec , 10 ))
113113
114114 // Call LimitReached handler
115115 return cfg .LimitReached (c )
@@ -131,7 +131,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
131131 // We can continue, update RateLimit headers
132132 c .Set (xRateLimitLimit , max )
133133 c .Set (xRateLimitRemaining , strconv .Itoa (remaining ))
134- c .Set (xRateLimitReset , strconv .FormatUint (expire , 10 ))
134+ c .Set (xRateLimitReset , strconv .FormatUint (resetInSec , 10 ))
135135
136136 return err
137137 }
0 commit comments