Skip to content

Commit d85ae2b

Browse files
Doc(limiter): clarify variable name 'expire' (#1742)
This clarifies the intent of variable 'expire' by renaming to the more understandable 'resetInSec'. All mentions are renamed as well.
1 parent 719992c commit d85ae2b

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

middleware/limiter/limited_fixed.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler {
6464
e.currHits++
6565

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

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

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

106106
return err
107107
}

middleware/limiter/limited_sliding.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)