client-go bucket rate limiter add maxDelay

Kubernetes-commit: 57ec8336195702dd0d5f6f19fd3907e2a6a615a8
This commit is contained in:
luyou86 2021-08-29 17:45:58 +08:00 committed by Kubernetes Publisher
parent cc7616029c
commit a6277bbabf
2 changed files with 48 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}
}
}