2014-09-26 04:24:44 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-26 04:24:44 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
2015-06-30 13:59:37 +00:00
|
|
|
import "github.com/juju/ratelimit"
|
2014-09-26 04:24:44 +00:00
|
|
|
|
|
|
|
type RateLimiter interface {
|
|
|
|
// CanAccept returns true if the rate is below the limit, false otherwise
|
|
|
|
CanAccept() bool
|
2015-04-01 21:57:30 +00:00
|
|
|
// Accept returns once a token becomes available.
|
|
|
|
Accept()
|
2014-09-26 04:24:44 +00:00
|
|
|
// Stop stops the rate limiter, subsequent calls to CanAccept will return false
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type tickRateLimiter struct {
|
2015-06-30 13:59:37 +00:00
|
|
|
limiter *ratelimit.Bucket
|
2014-09-26 04:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokenBucketRateLimiter creates a rate limiter which implements a token bucket approach.
|
|
|
|
// The rate limiter allows bursts of up to 'burst' to exceed the QPS, while still maintaining a
|
|
|
|
// smoothed qps rate of 'qps'.
|
2015-06-30 13:59:37 +00:00
|
|
|
// The bucket is initially filled with 'burst' tokens, and refills at a rate of 'qps'.
|
|
|
|
// The maximum number of tokens in the bucket is capped at 'burst'.
|
2014-09-26 04:24:44 +00:00
|
|
|
func NewTokenBucketRateLimiter(qps float32, burst int) RateLimiter {
|
2015-06-30 13:59:37 +00:00
|
|
|
limiter := ratelimit.NewBucketWithRate(float64(qps), int64(burst))
|
|
|
|
return &tickRateLimiter{limiter}
|
2014-09-26 04:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 15:13:13 +00:00
|
|
|
type fakeRateLimiter struct{}
|
|
|
|
|
|
|
|
func NewFakeRateLimiter() RateLimiter {
|
|
|
|
return &fakeRateLimiter{}
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:24:44 +00:00
|
|
|
func (t *tickRateLimiter) CanAccept() bool {
|
2015-06-30 13:59:37 +00:00
|
|
|
return t.limiter.TakeAvailable(1) == 1
|
2014-09-26 04:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 21:57:30 +00:00
|
|
|
// Accept will block until a token becomes available
|
|
|
|
func (t *tickRateLimiter) Accept() {
|
2015-06-30 13:59:37 +00:00
|
|
|
t.limiter.Wait(1)
|
2015-04-01 21:57:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:24:44 +00:00
|
|
|
func (t *tickRateLimiter) Stop() {
|
|
|
|
}
|
2015-04-02 15:13:13 +00:00
|
|
|
|
|
|
|
func (t *fakeRateLimiter) CanAccept() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *fakeRateLimiter) Stop() {}
|
|
|
|
|
|
|
|
func (t *fakeRateLimiter) Accept() {}
|