From 577896734de5b548f79174e33e0d1ff44e0c8fd6 Mon Sep 17 00:00:00 2001 From: Mario Manno Date: Fri, 13 Dec 2024 17:41:50 +0100 Subject: [PATCH] Add jitter to the pollingInterval of GitRepos --- .../cmd/controller/gitops/reconciler/gitjob_controller.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go index 550c7a5114..0352e595cb 100644 --- a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go +++ b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand/v2" "os" "reflect" "sort" @@ -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) @@ -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}