2020-03-04 15:37:17 +00:00
|
|
|
// Package jitter provides a random jitter
|
2024-09-20 17:54:17 +03:00
|
|
|
package jitter
|
2020-03-04 15:37:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/util/rand"
|
2020-03-04 15:37:17 +00:00
|
|
|
)
|
|
|
|
|
2021-08-04 00:37:56 +03:00
|
|
|
// Random returns a random time to jitter with max cap specified
|
|
|
|
func Random(d time.Duration) time.Duration {
|
2021-02-17 22:58:55 +03:00
|
|
|
var rng rand.Rand
|
|
|
|
v := rng.Float64() * float64(d.Nanoseconds())
|
2020-03-04 15:37:17 +00:00
|
|
|
return time.Duration(v)
|
|
|
|
}
|
2022-03-27 01:37:21 +03:00
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func RandomInterval(minTime, maxTime time.Duration) time.Duration {
|
2022-03-27 01:37:21 +03:00
|
|
|
var rng rand.Rand
|
2024-12-09 16:23:25 +03:00
|
|
|
return time.Duration(rng.Int63n(maxTime.Nanoseconds()-minTime.Nanoseconds())+minTime.Nanoseconds()) * time.Nanosecond
|
2022-03-27 01:37:21 +03:00
|
|
|
}
|