Skip to content

Commit

Permalink
chore: remove RetryAfter from the rate limiter
Browse files Browse the repository at this point in the history
Signed-off-by: Ernesto Alejandro Santana Hidalgo <[email protected]>
  • Loading branch information
nesty92 committed Nov 5, 2024
1 parent a0cb730 commit 255abd9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
3 changes: 1 addition & 2 deletions rueidislimiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ Returns a `Result` struct:

- `Allowed`: Whether the request is allowed.
- `Remaining`: Number of remaining requests in the current window.
- `RetryAfter`: Duration until the next allowed request (0 if allowed).
- `ResetAfter`: Duration until the current rate limit window resets.
- `ResetAt`: Unix timestamp at which the rate limit will reset.

#### `Allow`

Expand Down
23 changes: 5 additions & 18 deletions rueidislimiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type Result struct {
Allowed bool
// Remaining is the number of remaining tokens in the current window
Remaining int64
// ResetAfter is the duration until the rate limit resets
ResetAfter time.Duration
// RetryAfter is the duration after which the request may be retried (0 if allowed)
RetryAfter time.Duration
// ResetAt is the Unix timestamp at which the rate limit will reset
ResetAt int64
}

type RateLimiterClient interface {
Expand Down Expand Up @@ -149,21 +147,10 @@ func (l *rateLimiter) parseResult(array []rueidis.RedisMessage, now time.Time) (
remaining = 0
}

allowed := current < int64(l.limit)
resetAfter := time.Until(time.Unix(expiresAt, 0))
if resetAfter <= 0 {
resetAfter = l.window
}
retryAfter := resetAfter
if allowed {
retryAfter = 0
}

return Result{
Allowed: allowed,
Remaining: remaining,
ResetAfter: resetAfter,
RetryAfter: retryAfter,
Allowed: current <= int64(l.limit),
Remaining: remaining,
ResetAt: expiresAt,
}, nil
}

Expand Down
44 changes: 31 additions & 13 deletions rueidislimiter/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestRateLimiter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !result.Allowed || result.Remaining != 1 || result.RetryAfter != 0 {
t.Fatalf("Expected Allowed=true, Remaining=1, RetryAfter=0; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if !result.Allowed || result.Remaining != 1 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=true, Remaining=1, ResetAt >= now; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

Expand All @@ -59,21 +59,21 @@ func TestRateLimiter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !result.Allowed || result.Remaining != 3 || result.RetryAfter != 0 {
t.Fatalf("Expected Allowed=true, Remaining=3, RetryAfter=0; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if !result.Allowed || result.Remaining != 3 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=true, Remaining=3, ResetAt >= now; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

t.Run("Check denied after exceeding limit", func(t *testing.T) {
key := randStr()
generateLoad(t, limiter, key, 3)
generateLoad(t, limiter, key, 4)

result, err := limiter.Check(context.Background(), key)
if err != nil {
t.Fatal(err)
}
if result.Allowed || result.Remaining != 0 || result.RetryAfter <= 0 {
t.Fatalf("Expected Allowed=false, Remaining=0, RetryAfter > 0; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if result.Allowed || result.Remaining != 0 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=false, Remaining=0, ResetAt >= now; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

Expand All @@ -86,8 +86,26 @@ func TestRateLimiter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !result.Allowed || result.Remaining != 3 || result.RetryAfter != 0 {
t.Fatalf("Expected Allowed=true, Remaining=3, RetryAfter=0 after reset; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if !result.Allowed || result.Remaining != 3 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=true, Remaining=3, ResetAt=0 after reset; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

t.Run("AllowN defaults", func(t *testing.T) {
limiter, err := rueidislimiter.NewRateLimiter(rueidislimiter.RateLimiterOption{
ClientBuilder: func(option rueidis.ClientOption) (rueidis.Client, error) {
return client, nil
},
})
if err != nil {
t.Fatal(err)
}
result, err := limiter.AllowN(context.Background(), randStr(), 1)
if err != nil {
t.Fatal(err)
}
if !result.Allowed || result.Remaining != 0 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=true, Remaining=0, ResetAt >= now; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

Expand All @@ -97,8 +115,8 @@ func TestRateLimiter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !result.Allowed || result.Remaining != 2 || result.RetryAfter != 0 {
t.Fatalf("Expected Allowed=true, Remaining=2, RetryAfter=0; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if !result.Allowed || result.Remaining != 2 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=true, Remaining=2, ResetAt=0; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

Expand All @@ -110,8 +128,8 @@ func TestRateLimiter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if result.Allowed || result.Remaining != 0 || result.RetryAfter <= 0 {
t.Fatalf("Expected Allowed=false, Remaining=0, RetryAfter > 0; got Allowed=%v, Remaining=%v, RetryAfter=%v", result.Allowed, result.Remaining, result.RetryAfter)
if result.Allowed || result.Remaining != 0 || result.ResetAt < time.Now().Unix() {
t.Fatalf("Expected Allowed=false, Remaining=0, ResetAt > 0; got Allowed=%v, Remaining=%v, ResetAt=%v", result.Allowed, result.Remaining, result.ResetAt)
}
})

Expand Down

0 comments on commit 255abd9

Please sign in to comment.