Skip to content

Commit 1b762ad

Browse files
author
Shayon Mukherjee
committed
Jitter/stagger in milliseconds when performing randomized execution
This is mostly to have more room for randomness/variability when performing sleep operation randomly. This takes the seconds input and performs sleep operation on a ms figure between [1000ms, xMs]
1 parent 4a1c3ff commit 1b762ad

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

exec/exec.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919

2020
heartBeatDuration = 1 // seconds
2121

22-
minRandomSleep = 1 // seconds
22+
minRandomSleep = 1000 // milliseconds - 1second
2323
)
2424

2525
var execCommand = exec.Command
@@ -126,11 +126,13 @@ func runExec(command string) (string, error) {
126126
// randomized effect by sleeping for the random interval.
127127
func randomizeSleep(i int) {
128128
rand.Seed(time.Now().UnixNano())
129-
r := minRandomSleep + rand.Intn(i-minRandomSleep+1) // ensures that the random range is [1, i]. To avoid sleeping for 0 seconds.
129+
ms := i * 1000
130+
offsetMS := 100 // milliseconds
131+
r := minRandomSleep + rand.Intn(ms-minRandomSleep+offsetMS) // ensures that the random range is [1, i]. To avoid sleeping for 0 seconds (0 ms).
130132

131-
logrus.Infof("Randomized execution. Sleeping for %d seconds.", r)
133+
logrus.Infof("Randomized execution. Sleeping for %d milliseconds.", r)
132134

133-
time.Sleep(time.Duration(r) * time.Second)
135+
time.Sleep(time.Duration(r) * time.Millisecond)
134136
}
135137

136138
// sleepBy takes a int input, and sleeps for that duration

0 commit comments

Comments
 (0)