25 lines
572 B
Go
25 lines
572 B
Go
package router
|
|
|
|
import (
|
|
cryptorand "crypto/rand"
|
|
"math/big"
|
|
)
|
|
|
|
// CryptoRandom is the production source for weighted selection. Returning the
|
|
// upper bound on a rare entropy failure makes SelectCandidates reject the
|
|
// value rather than silently biasing traffic to a route member.
|
|
type CryptoRandom struct{}
|
|
|
|
func (CryptoRandom) Uint64n(limit uint64) uint64 {
|
|
if limit == 0 {
|
|
return 0
|
|
}
|
|
value, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetUint64(limit))
|
|
if err != nil {
|
|
return limit
|
|
}
|
|
return value.Uint64()
|
|
}
|
|
|
|
var _ RandomSource = CryptoRandom{}
|