Skip to content

Commit

Permalink
Remove RateLimiter dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gabesaba committed May 13, 2024
1 parent bb110db commit b3b0b2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
30 changes: 13 additions & 17 deletions pkg/util/wait/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
)

Expand All @@ -21,9 +20,8 @@ func UntilWithBackoff(ctx context.Context, f func(context.Context) SpeedSignal)

func untilWithBackoff(ctx context.Context, f func(context.Context) SpeedSignal, timer clock.Timer) {
mgr := speedyBackoffManager{
backingOff: false,
rateLimiter: workqueue.NewItemExponentialFailureRateLimiter(initialBackoff, maxBackoff),
timer: timer,
backoff: noBackoff,
timer: timer,
}
wait.BackoffUntil(func() {
mgr.toggleBackoff(f(ctx))
Expand All @@ -48,28 +46,26 @@ const (
func (s *speedyBackoffManager) toggleBackoff(speedSignal SpeedSignal) {
switch speedSignal {
case KeepGoing:
if s.backingOff {
s.backingOff = false
s.rateLimiter.Forget("")
}
s.backoff = noBackoff
case SlowDown:
s.backingOff = true
if s.backoff == noBackoff {
s.backoff = initialBackoff
}
}
}

type speedyBackoffManager struct {
backingOff bool
rateLimiter workqueue.RateLimiter
timer clock.Timer
backoff time.Duration
timer clock.Timer
}

var _ wait.BackoffManager = (*speedyBackoffManager)(nil)

func (s speedyBackoffManager) Backoff() clock.Timer {
if s.backingOff {
s.timer.Reset(s.rateLimiter.When(""))
} else {
s.timer.Reset(noBackoff)
func (s *speedyBackoffManager) Backoff() clock.Timer {
s.timer.Reset(s.backoff)
s.backoff *= 2
if s.backoff > maxBackoff {
s.backoff = maxBackoff
}
return s.timer
}
5 changes: 5 additions & 0 deletions pkg/util/wait/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func TestUntilWithBackoff(t *testing.T) {
signals: []SpeedSignal{KeepGoing, KeepGoing, KeepGoing, KeepGoing},
expected: []time.Duration{ms(0), ms(0), ms(0), ms(0), ms(0)},
},
{
name: "reset before reaching max backoff",
signals: []SpeedSignal{SlowDown, SlowDown, SlowDown, KeepGoing, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, KeepGoing},
expected: []time.Duration{ms(0), ms(1), ms(2), ms(4), ms(0), ms(1), ms(2), ms(4), ms(8), ms(16), ms(32), ms(64), ms(0)},
},
{
name: "double until max then reset",
signals: []SpeedSignal{SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, SlowDown, KeepGoing},
Expand Down

0 comments on commit b3b0b2f

Please sign in to comment.