Skip to content

Commit

Permalink
k8s/ctrl: added jitter to exponentional backoff in reconcile loops
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Nov 24, 2024
1 parent 583a288 commit 0186569
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/k8s/ctrl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (ctrl Instance) process(ctx context.Context, events chan Event, handle Hand
shouldRequeue := result.Requeue || result.RequeueAfter > 0 || err != nil

if shouldRequeue && result.RequeueAfter == 0 {
result.RequeueAfter = min(time.Duration(powInt(2, event.attempts))*time.Second, 15*time.Minute)
result.RequeueAfter = withJitter(min(time.Duration(powInt(2, event.attempts))*time.Second, 15*time.Minute), 0.10)
}

if shouldRequeue {
Expand Down Expand Up @@ -228,3 +228,9 @@ func Client(ctx context.Context) *k8s.Client {
client, _ := ctx.Value(clientKey{}).(*k8s.Client)
return client
}

func withJitter(duration time.Duration, percent float64) time.Duration {
offset := float64(duration) * percent
jitter := 2 * offset * rand.Float64()
return time.Duration(float64(duration) - offset + jitter).Round(time.Second)
}

0 comments on commit 0186569

Please sign in to comment.