Skip to content

Commit 6ec1d79

Browse files
committed
Correct logic for rate limiter
1 parent c1074de commit 6ec1d79

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

pkg/rate/limiter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ func (l *Limiter) WaitN(ctx context.Context, n int) error {
4242
return fmt.Errorf("rate: Wait (n=%d) exceed limiter %d", n, l.limit)
4343
}
4444

45-
now := time.Now()
46-
4745
// Get the oldest request in queue for waiting.
4846
var (
4947
shouldWait bool
@@ -56,7 +54,7 @@ func (l *Limiter) WaitN(ctx context.Context, n int) error {
5654

5755
// Wait if rate limit is reached.
5856
if shouldWait {
59-
waitDuration := l.period - now.Sub(oldest)
57+
waitDuration := l.period - time.Since(oldest)
6058
if waitDuration > 0 {
6159
timer := time.NewTimer(waitDuration)
6260
defer timer.Stop()
@@ -73,7 +71,7 @@ func (l *Limiter) WaitN(ctx context.Context, n int) error {
7371

7472
// Add new requests to queue.
7573
for range n {
76-
l.addRequest(now)
74+
l.addRequest(time.Now())
7775
}
7876

7977
return nil

0 commit comments

Comments
 (0)