Update to avoid pseudo-random number (#5225)

* Update to avoid pseudo-random number

This PR update the usage of rand so that non-global seed is used.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

* Add concurrency-safe random source

See https://stackoverflow.com/questions/48958886/how-to-create-a-thread-safe-rand-source

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2022-03-16 09:24:58 -07:00
committed by GitHub
parent c3b30cc3ef
commit aa7818e1d3
4 changed files with 52 additions and 8 deletions

View File

@@ -1,8 +1,10 @@
package forward
import (
"math/rand"
"sync/atomic"
"time"
"github.com/coredns/coredns/plugin/pkg/rand"
)
// Policy defines a policy we use for selecting upstreams.
@@ -21,13 +23,13 @@ func (r *random) List(p []*Proxy) []*Proxy {
case 1:
return p
case 2:
if rand.Int()%2 == 0 {
if rn.Int()%2 == 0 {
return []*Proxy{p[1], p[0]} // swap
}
return p
}
perms := rand.Perm(len(p))
perms := rn.Perm(len(p))
rnd := make([]*Proxy, len(p))
for i, p1 := range perms {
@@ -62,3 +64,5 @@ func (r *sequential) String() string { return "sequential" }
func (r *sequential) List(p []*Proxy) []*Proxy {
return p
}
var rn = rand.New(time.Now().UnixNano())