Codebase list golang-github-juju-ratelimit / 9657ea0
Import upstream version 1.0.1+git20191002.1.f60b320 Debian Janitor 2 years ago
3 changed file(s) with 40 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
5959 clock resolution, at high rates, the actual rate may be up to 1% different from
6060 the specified rate.
6161
62 #### func (*Bucket) Available
63
64 ```go
65 func (tb *Bucket) Available() int64
66 ```
67 Available returns the number of available tokens. It will be negative
68 when there are consumers waiting for tokens. Note that if this
69 returns greater than zero, it does not guarantee that calls that take
70 tokens from the buffer will succeed, as the number of available
71 tokens could have changed in the meantime. This method is intended
72 primarily for metrics reporting and debugging.
73
6274 #### func (*Bucket) Rate
6375
6476 ```go
309309 // available in the bucket at the given time, which must
310310 // be in the future (positive) with respect to tb.latestTick.
311311 func (tb *Bucket) adjustavailableTokens(tick int64) {
312 lastTick := tb.latestTick
313 tb.latestTick = tick
312314 if tb.availableTokens >= tb.capacity {
313315 return
314316 }
315 tb.availableTokens += (tick - tb.latestTick) * tb.quantum
317 tb.availableTokens += (tick - lastTick) * tb.quantum
316318 if tb.availableTokens > tb.capacity {
317319 tb.availableTokens = tb.capacity
318320 }
319 tb.latestTick = tick
320321 return
321322 }
322323
381381
382382 }
383383
384 func TestNoBonusTokenAfterBucketIsFull(t *testing.T) {
385 tb := NewBucketWithQuantum(time.Second*1, 100, 20)
386 curAvail := tb.Available()
387 if curAvail != 100 {
388 t.Fatalf("initially: actual available = %d, expected = %d", curAvail, 100)
389 }
390
391 time.Sleep(time.Second * 5)
392
393 curAvail = tb.Available()
394 if curAvail != 100 {
395 t.Fatalf("after pause: actual available = %d, expected = %d", curAvail, 100)
396 }
397
398 cnt := tb.TakeAvailable(100)
399 if cnt != 100 {
400 t.Fatalf("taking: actual taken count = %d, expected = %d", cnt, 100)
401 }
402
403 curAvail = tb.Available()
404 if curAvail != 0 {
405 t.Fatalf("after taken: actual available = %d, expected = %d", curAvail, 0)
406 }
407 }
408
384409 func BenchmarkWait(b *testing.B) {
385410 tb := NewBucket(1, 16*1024)
386411 for i := b.N - 1; i >= 0; i-- {