Skip to content
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

Doc(limiter): clarify variable name 'expire' #1742

Merged
merged 1 commit into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions middleware/limiter/limited_fixed.go
Expand Up @@ -64,7 +64,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler {
e.currHits++

// Calculate when it resets in seconds
expire := e.exp - ts
resetInSec := e.exp - ts

// Set how many hits we have left
remaining := cfg.Max - e.currHits
Expand All @@ -79,7 +79,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler {
if remaining < 0 {
// Return response with Retry-After header
// https://tools.ietf.org/html/rfc6584
c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(expire, 10))
c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(resetInSec, 10))

// Call LimitReached handler
return cfg.LimitReached(c)
Expand All @@ -101,7 +101,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler {
// We can continue, update RateLimit headers
c.Set(xRateLimitLimit, max)
c.Set(xRateLimitRemaining, strconv.Itoa(remaining))
c.Set(xRateLimitReset, strconv.FormatUint(expire, 10))
c.Set(xRateLimitReset, strconv.FormatUint(resetInSec, 10))

return err
}
Expand Down
22 changes: 11 additions & 11 deletions middleware/limiter/limited_sliding.go
Expand Up @@ -77,10 +77,10 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
e.currHits++

// Calculate when it resets in seconds
expire := e.exp - ts
resetInSec := e.exp - ts

// weight = time until current window reset / total window length
weight := float64(expire) / float64(expiration)
weight := float64(resetInSec) / float64(expiration)

// rate = request count in previous window - weight + request count in current window
rate := int(float64(e.prevHits)*weight) + e.currHits
Expand All @@ -89,18 +89,18 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
remaining := cfg.Max - rate

// Update storage. Garbage collect when the next window ends.
// |-------------------------|-------------------------|
// ^ ^ ^ ^
// ts e.exp End sample window End next window
// <----------->
// expire
// expire = e.exp - ts - time until end of current window.
// |--------------------------|--------------------------|
// ^ ^ ^ ^
// ts e.exp End sample window End next window
// <------------>
// resetInSec
// resetInSec = e.exp - ts - time until end of current window.
// duration + expiration = end of next window.
// Because we don't want to garbage collect in the middle of a window
// we add the expiration to the duration.
// Otherwise after the end of "sample window", attackers could launch
// a new request with the full window length.
manager.set(key, e, time.Duration(expire+expiration)*time.Second)
manager.set(key, e, time.Duration(resetInSec+expiration)*time.Second)

// Unlock entry
mux.Unlock()
Expand All @@ -109,7 +109,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
if remaining < 0 {
// Return response with Retry-After header
// https://tools.ietf.org/html/rfc6584
c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(expire, 10))
c.Set(fiber.HeaderRetryAfter, strconv.FormatUint(resetInSec, 10))

// Call LimitReached handler
return cfg.LimitReached(c)
Expand All @@ -131,7 +131,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler {
// We can continue, update RateLimit headers
c.Set(xRateLimitLimit, max)
c.Set(xRateLimitRemaining, strconv.Itoa(remaining))
c.Set(xRateLimitReset, strconv.FormatUint(expire, 10))
c.Set(xRateLimitReset, strconv.FormatUint(resetInSec, 10))

return err
}
Expand Down