Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Fixed leaky bucket ResetTime invalid on first request
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Dec 16, 2020
1 parent 4d87ed8 commit 79420e9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func leakyBucket(s Store, c Cache, r *RateLimitReq) (resp *RateLimitResp, err er
Status: Status_UNDER_LIMIT,
Limit: r.Limit,
Remaining: r.Limit - r.Hits,
ResetTime: duration / r.Limit,
ResetTime: now + duration / r.Limit,
}

// Client could be requesting that we start with the bucket OVER_LIMIT
Expand Down
3 changes: 2 additions & 1 deletion functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func TestLeakyBucket(t *testing.T) {
},
}

now := clock.Now()
for i, test := range tests {
resp, err := client.GetRateLimits(context.Background(), &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{
Expand All @@ -212,7 +213,7 @@ func TestLeakyBucket(t *testing.T) {
assert.Equal(t, test.Status, rl.Status, i)
assert.Equal(t, test.Remaining, rl.Remaining, i)
assert.Equal(t, int64(5), rl.Limit, i)
assert.True(t, rl.ResetTime != 0)
assert.True(t, rl.ResetTime > now.Unix())
clock.Advance(test.Sleep)
}
}
Expand Down

0 comments on commit 79420e9

Please sign in to comment.