Skip to content

Commit

Permalink
Add jitter to the pollingInterval of GitRepos
Browse files Browse the repository at this point in the history
  • Loading branch information
manno committed Dec 16, 2024
1 parent 93801e5 commit 5778967
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand/v2"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -187,6 +188,7 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
result := reconcile.Result{}
if repoPolled {
result = reconcile.Result{RequeueAfter: getPollingIntervalDuration(gitrepo)}
result.RequeueAfter = addJitter(result.RequeueAfter)
}

res, err := r.manageGitJob(ctx, logger, gitrepo, oldCommit, repoPolled, result)
Expand All @@ -206,6 +208,12 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return result, nil
}

// addJitter to the requeue time to avoid thundering herd
// generate a random number between -10% and +10% of the duration
func addJitter(d time.Duration) time.Duration {
return d + time.Duration(rand.Int64N(int64(d)/5)-int64(d)/10)
}

// manageGitJob is responsible for creating, updating and deleting the GitJob and setting the GitRepo's status accordingly
func (r *GitJobReconciler) manageGitJob(ctx context.Context, logger logr.Logger, gitrepo *v1alpha1.GitRepo, oldCommit string, repoPolled bool, oldResult reconcile.Result) (reconcile.Result, error) {
name := types.NamespacedName{Namespace: gitrepo.Namespace, Name: gitrepo.Name}
Expand Down

0 comments on commit 5778967

Please sign in to comment.