throttle.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flowcontrol
  14. import (
  15. "sync"
  16. "github.com/juju/ratelimit"
  17. )
  18. type RateLimiter interface {
  19. // TryAccept returns true if a token is taken immediately. Otherwise,
  20. // it returns false.
  21. TryAccept() bool
  22. // Accept returns once a token becomes available.
  23. Accept()
  24. // Stop stops the rate limiter, subsequent calls to CanAccept will return false
  25. Stop()
  26. // Saturation returns a percentage number which describes how saturated
  27. // this rate limiter is.
  28. // Usually we use token bucket rate limiter. In that case,
  29. // 1.0 means no tokens are available; 0.0 means we have a full bucket of tokens to use.
  30. Saturation() float64
  31. // QPS returns QPS of this rate limiter
  32. QPS() float32
  33. }
  34. type tokenBucketRateLimiter struct {
  35. limiter *ratelimit.Bucket
  36. qps float32
  37. }
  38. // NewTokenBucketRateLimiter creates a rate limiter which implements a token bucket approach.
  39. // The rate limiter allows bursts of up to 'burst' to exceed the QPS, while still maintaining a
  40. // smoothed qps rate of 'qps'.
  41. // The bucket is initially filled with 'burst' tokens, and refills at a rate of 'qps'.
  42. // The maximum number of tokens in the bucket is capped at 'burst'.
  43. func NewTokenBucketRateLimiter(qps float32, burst int) RateLimiter {
  44. limiter := ratelimit.NewBucketWithRate(float64(qps), int64(burst))
  45. return &tokenBucketRateLimiter{
  46. limiter: limiter,
  47. qps: qps,
  48. }
  49. }
  50. func (t *tokenBucketRateLimiter) TryAccept() bool {
  51. return t.limiter.TakeAvailable(1) == 1
  52. }
  53. func (t *tokenBucketRateLimiter) Saturation() float64 {
  54. capacity := t.limiter.Capacity()
  55. avail := t.limiter.Available()
  56. return float64(capacity-avail) / float64(capacity)
  57. }
  58. // Accept will block until a token becomes available
  59. func (t *tokenBucketRateLimiter) Accept() {
  60. t.limiter.Wait(1)
  61. }
  62. func (t *tokenBucketRateLimiter) Stop() {
  63. }
  64. func (t *tokenBucketRateLimiter) QPS() float32 {
  65. return t.qps
  66. }
  67. type fakeAlwaysRateLimiter struct{}
  68. func NewFakeAlwaysRateLimiter() RateLimiter {
  69. return &fakeAlwaysRateLimiter{}
  70. }
  71. func (t *fakeAlwaysRateLimiter) TryAccept() bool {
  72. return true
  73. }
  74. func (t *fakeAlwaysRateLimiter) Saturation() float64 {
  75. return 0
  76. }
  77. func (t *fakeAlwaysRateLimiter) Stop() {}
  78. func (t *fakeAlwaysRateLimiter) Accept() {}
  79. func (t *fakeAlwaysRateLimiter) QPS() float32 {
  80. return 1
  81. }
  82. type fakeNeverRateLimiter struct {
  83. wg sync.WaitGroup
  84. }
  85. func NewFakeNeverRateLimiter() RateLimiter {
  86. rl := fakeNeverRateLimiter{}
  87. rl.wg.Add(1)
  88. return &rl
  89. }
  90. func (t *fakeNeverRateLimiter) TryAccept() bool {
  91. return false
  92. }
  93. func (t *fakeNeverRateLimiter) Saturation() float64 {
  94. return 1
  95. }
  96. func (t *fakeNeverRateLimiter) Stop() {
  97. t.wg.Done()
  98. }
  99. func (t *fakeNeverRateLimiter) Accept() {
  100. t.wg.Wait()
  101. }
  102. func (t *fakeNeverRateLimiter) QPS() float32 {
  103. return 1
  104. }