micro/util/jitter/random.go

21 lines
559 B
Go
Raw Normal View History

2020-03-04 15:37:17 +00:00
// Package jitter provides a random jitter
package jitter // import "go.unistack.org/micro/v4/util/jitter"
2020-03-04 15:37:17 +00:00
import (
"time"
"go.unistack.org/micro/v4/util/rand"
2020-03-04 15:37:17 +00:00
)
// 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())
2020-03-04 15:37:17 +00:00
return time.Duration(v)
}
func RandomInterval(min, max time.Duration) time.Duration {
var rng rand.Rand
return time.Duration(rng.Int63n(max.Nanoseconds()-min.Nanoseconds())+min.Nanoseconds()) * time.Nanosecond
}