Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/mirror/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,8 @@ func runGitCommand(ctx context.Context, log *slog.Logger, envs []string, cwd str

return stdout, nil
}

// jitter returns a time.Duration between duration and duration + maxFactor * duration.
func jitter(duration time.Duration, maxFactor float64) time.Duration {
return duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
}
35 changes: 35 additions & 0 deletions pkg/mirror/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -375,3 +376,37 @@ t1643d7874890dca5982facfba9c4f24da53876e9 4c286e182bc4d1832a8739b18c19ecaf9262c3
})
}
}

func TestJitter(t *testing.T) {
type args struct {
duration time.Duration
maxFactor float64
}
tests := []struct {
name string
args args
minWant time.Duration
maxWant time.Duration
}{
{"1", args{10 * time.Second, 0.1}, 10 * time.Second, 11 * time.Second},
{"2", args{10 * time.Second, 0.5}, 10 * time.Second, 15 * time.Second},
{"3", args{10 * time.Second, 0.0}, 10 * time.Second, 10 * time.Second},
{"4", args{30 * time.Second, 0.1}, 30 * time.Second, 33 * time.Second},
{"5", args{30 * time.Second, 0.5}, 30 * time.Second, 45 * time.Second},
{"6", args{30 * time.Second, 0.0}, 30 * time.Second, 30 * time.Second},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// since we are using rand test values 10 times
for i := 0; i < 10; i++ {
got := jitter(tt.args.duration, tt.args.maxFactor)
if got < tt.minWant {
t.Errorf("jitter() = %v, min-want %v", got, tt.minWant)
}
if got > tt.maxWant {
t.Errorf("jitter() = %v, max-want %v", got, tt.maxWant)
}
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/mirror/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (r *Repository) StartLoop(ctx context.Context) {
}
recordGitMirror(r.gitURL.Repo, err == nil)

t := time.NewTimer(r.interval)
t := time.NewTimer(jitter(r.interval, 0.2))
select {
case <-t.C:
case <-ctx.Done():
Expand Down
Loading