mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-24 06:07:48 +00:00
client-go bucket rate limiter add maxDelay
Kubernetes-commit: 57ec8336195702dd0d5f6f19fd3907e2a6a615a8
This commit is contained in:
parent
cc7616029c
commit
a6277bbabf
@ -209,3 +209,30 @@ func (r *MaxOfRateLimiter) Forget(item interface{}) {
|
||||
limiter.Forget(item)
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxWaitRateLimiter have maxDelay which avoids waiting too long
|
||||
type WithMaxWaitRateLimiter struct {
|
||||
limiter RateLimiter
|
||||
maxDelay time.Duration
|
||||
}
|
||||
|
||||
func NewWithMaxWaitRateLimiter(limiter RateLimiter, maxDelay time.Duration) RateLimiter {
|
||||
return &WithMaxWaitRateLimiter{limiter: limiter, maxDelay: maxDelay}
|
||||
}
|
||||
|
||||
func (w WithMaxWaitRateLimiter) When(item interface{}) time.Duration {
|
||||
delay := w.limiter.When(item)
|
||||
if delay > w.maxDelay {
|
||||
return w.maxDelay
|
||||
}
|
||||
|
||||
return delay
|
||||
}
|
||||
|
||||
func (w WithMaxWaitRateLimiter) Forget(item interface{}) {
|
||||
w.limiter.Forget(item)
|
||||
}
|
||||
|
||||
func (w WithMaxWaitRateLimiter) NumRequeues(item interface{}) int {
|
||||
return w.limiter.NumRequeues(item)
|
||||
}
|
||||
|
@ -182,3 +182,24 @@ func TestMaxOfRateLimiter(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestWithMaxWaitRateLimiter(t *testing.T) {
|
||||
limiter := NewWithMaxWaitRateLimiter(DefaultControllerRateLimiter(), 500*time.Second)
|
||||
for i := 0; i < 100; i++ {
|
||||
if e, a := 5*time.Millisecond, limiter.When(i); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 100; i < 5100; i++ {
|
||||
if e, a := 500*time.Second, limiter.When(i); e < a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 5100; i < 5200; i++ {
|
||||
if e, a := 500*time.Second, limiter.When(i); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user