Skip to content

Commit a7c1da1

Browse files
committed
Update package rate
1 parent 77b1cbf commit a7c1da1

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

lib/rate/rate.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,16 @@ type Limits struct {
1919
step time.Duration
2020
}
2121

22-
// Peek glances there are enough resources (n) available.
23-
func (l *Limits) Peek(n uint64) bool {
24-
l.mu.Lock()
25-
defer l.mu.Unlock()
26-
cycles := uint64(time.Since(l.last) / l.step)
27-
if cycles > 0 {
28-
l.last = l.last.Add(l.step * time.Duration(cycles))
29-
if cycles > math.MaxUint64/l.addition {
30-
log.Panicln("rate: overflow")
31-
}
32-
if l.size > math.MaxUint64-l.addition*cycles {
33-
log.Panicln("rate: overflow")
34-
}
35-
l.size = l.size + l.addition*cycles
36-
l.size = min(l.size, l.capacity)
37-
}
38-
return l.size >= n
39-
}
40-
4122
// Wait ensures there are enough resources (n) available, blocking if necessary.
4223
func (l *Limits) Wait(n uint64) {
4324
l.mu.Lock()
4425
defer l.mu.Unlock()
45-
cycles := uint64(time.Since(l.last) / l.step)
26+
curr := time.Now()
27+
if curr.Before(l.last) {
28+
l.last = curr
29+
}
30+
diff := curr.Sub(l.last)
31+
cycles := uint64(diff / l.step)
4632
if cycles > 0 {
4733
l.last = l.last.Add(l.step * time.Duration(cycles))
4834
if cycles > math.MaxUint64/l.addition {

0 commit comments

Comments
 (0)