Skip to content

Commit

Permalink
Enhance error handling for randomSleep
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Oct 28, 2024
2 parents 679efa7 + a0fdb07 commit c44086f
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/core/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,22 @@ func GenRandomPassword(length int) string {
}

// RandomSleep is func to make a caller waits for during random time seconds (random value within x~y)
func RandomSleep(from int, to int) {
if from > to {
tmp := from
from = to
to = tmp
func RandomSleep(from, to int) {
const minSleepTime = 1

if from == 0 && to == 0 {
from = minSleepTime
to = minSleepTime
} else if from > to {
from, to = to, from
}

t := to - from
rand.Seed(time.Now().UnixNano())
n := rand.Intn(t * 1000)
if t == 0 {
t = minSleepTime
}

n := rand.Intn(t*1000) + from*1000
time.Sleep(time.Duration(n) * time.Millisecond)
}

Expand Down

0 comments on commit c44086f

Please sign in to comment.