Skip to content

Commit 192e8b9

Browse files
committed
cherrypick patch from github.com/Snawoot/xtime
1 parent 0c01411 commit 192e8b9

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

rate/rate.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Every(interval time.Duration) Limit {
5757
type Limiter struct {
5858
mu sync.Mutex
5959
limit Limit
60-
burst int
60+
burst int64
6161
tokens float64
6262
// last is the last time the limiter's tokens field was updated
6363
last time.Time
@@ -76,7 +76,7 @@ func (lim *Limiter) Limit() Limit {
7676
// that can be consumed in a single call to Allow, Reserve, or Wait, so higher
7777
// Burst values allow more events to happen at once.
7878
// A zero Burst allows no events, unless limit == Inf.
79-
func (lim *Limiter) Burst() int {
79+
func (lim *Limiter) Burst() int64 {
8080
lim.mu.Lock()
8181
defer lim.mu.Unlock()
8282
return lim.burst
@@ -97,7 +97,7 @@ func (lim *Limiter) Tokens() float64 {
9797

9898
// NewLimiter returns a new Limiter that allows events up to rate r and permits
9999
// bursts of at most b tokens.
100-
func NewLimiter(r Limit, b int) *Limiter {
100+
func NewLimiter(r Limit, b int64) *Limiter {
101101
return &Limiter{
102102
limit: r,
103103
burst: b,
@@ -113,7 +113,7 @@ func (lim *Limiter) Allow() bool {
113113
// AllowN reports whether n events may happen at time t.
114114
// Use this method if you intend to drop / skip events that exceed the rate limit.
115115
// Otherwise use Reserve or Wait.
116-
func (lim *Limiter) AllowN(t time.Time, n int) bool {
116+
func (lim *Limiter) AllowN(t time.Time, n int64) bool {
117117
return lim.reserveN(t, n, 0).ok
118118
}
119119

@@ -122,7 +122,7 @@ func (lim *Limiter) AllowN(t time.Time, n int) bool {
122122
type Reservation struct {
123123
ok bool
124124
lim *Limiter
125-
tokens int
125+
tokens int64
126126
timeToAct time.Time
127127
// This is the Limit at reservation time, it can change later.
128128
limit Limit
@@ -224,7 +224,7 @@ func (lim *Limiter) Reserve() *Reservation {
224224
// Use this method if you wish to wait and slow down in accordance with the rate limit without dropping events.
225225
// If you need to respect a deadline or cancel the delay, use Wait instead.
226226
// To drop or skip events exceeding rate limit, use Allow instead.
227-
func (lim *Limiter) ReserveN(t time.Time, n int) *Reservation {
227+
func (lim *Limiter) ReserveN(t time.Time, n int64) *Reservation {
228228
r := lim.reserveN(t, n, InfDuration)
229229
return &r
230230
}
@@ -238,7 +238,7 @@ func (lim *Limiter) Wait(ctx context.Context) (err error) {
238238
// It returns an error if n exceeds the Limiter's burst size, the Context is
239239
// canceled, or the expected wait time exceeds the Context's Deadline.
240240
// The burst limit is ignored if the rate limit is Inf.
241-
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
241+
func (lim *Limiter) WaitN(ctx context.Context, n int64) (err error) {
242242
// The test code calls lim.wait with a fake timer generator.
243243
// This is the real timer generator.
244244
newTimer := func(d time.Duration) (<-chan time.Time, func() bool, func()) {
@@ -250,7 +250,7 @@ func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
250250
}
251251

252252
// wait is the internal implementation of WaitN.
253-
func (lim *Limiter) wait(ctx context.Context, n int, t time.Time, newTimer func(d time.Duration) (<-chan time.Time, func() bool, func())) error {
253+
func (lim *Limiter) wait(ctx context.Context, n int64, t time.Time, newTimer func(d time.Duration) (<-chan time.Time, func() bool, func())) error {
254254
lim.mu.Lock()
255255
burst := lim.burst
256256
limit := lim.limit
@@ -315,12 +315,12 @@ func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit) {
315315
}
316316

317317
// SetBurst is shorthand for SetBurstAt(time.Now(), newBurst).
318-
func (lim *Limiter) SetBurst(newBurst int) {
318+
func (lim *Limiter) SetBurst(newBurst int64) {
319319
lim.SetBurstAt(time.Now(), newBurst)
320320
}
321321

322322
// SetBurstAt sets a new burst size for the limiter.
323-
func (lim *Limiter) SetBurstAt(t time.Time, newBurst int) {
323+
func (lim *Limiter) SetBurstAt(t time.Time, newBurst int64) {
324324
lim.mu.Lock()
325325
defer lim.mu.Unlock()
326326

@@ -334,7 +334,7 @@ func (lim *Limiter) SetBurstAt(t time.Time, newBurst int) {
334334
// reserveN is a helper method for AllowN, ReserveN, and WaitN.
335335
// maxFutureReserve specifies the maximum reservation wait duration allowed.
336336
// reserveN returns Reservation, not *Reservation, to avoid allocation in AllowN and WaitN.
337-
func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration) Reservation {
337+
func (lim *Limiter) reserveN(t time.Time, n int64, maxFutureReserve time.Duration) Reservation {
338338
lim.mu.Lock()
339339
defer lim.mu.Unlock()
340340

rate/rate_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var (
6767
type allow struct {
6868
t time.Time
6969
toks float64
70-
n int
70+
n int64
7171
ok bool
7272
}
7373

@@ -304,7 +304,7 @@ func TestLongRunningQPS(t *testing.T) {
304304
// A request provides the arguments to lim.reserveN(t, n) and the expected results (act, ok).
305305
type request struct {
306306
t time.Time
307-
n int
307+
n int64
308308
act time.Time
309309
ok bool
310310
}
@@ -482,7 +482,7 @@ func TestReserveMax(t *testing.T) {
482482
type wait struct {
483483
name string
484484
ctx context.Context
485-
n int
485+
n int64
486486
delay int // in multiples of d
487487
nilErr bool
488488
}
@@ -600,7 +600,7 @@ func BenchmarkAllowN(b *testing.B) {
600600
}
601601

602602
func BenchmarkWaitNNoDelay(b *testing.B) {
603-
lim := NewLimiter(Limit(b.N), b.N)
603+
lim := NewLimiter(Limit(b.N), int64(b.N))
604604
ctx := context.Background()
605605
b.ReportAllocs()
606606
b.ResetTimer()

0 commit comments

Comments
 (0)