micro/util/jitter/random.go
Vasiliy Tolstov f3f2a9b737
Some checks failed
coverage / build (push) Failing after 56s
test / test (push) Successful in 2m30s
move to v4
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-01-25 15:48:10 +03:00

21 lines
530 B
Go

// Package jitter provides a random jitter
package jitter
import (
"time"
"go.unistack.org/micro/v4/util/rand"
)
// Random returns a random time to jitter with max cap specified
func Random(d time.Duration) time.Duration {
var rng rand.Rand
v := rng.Float64() * float64(d.Nanoseconds())
return time.Duration(v)
}
func RandomInterval(minTime, maxTime time.Duration) time.Duration {
var rng rand.Rand
return time.Duration(rng.Int63n(maxTime.Nanoseconds()-minTime.Nanoseconds())+minTime.Nanoseconds()) * time.Nanosecond
}