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:
parent
c3b30cc3ef
commit
aa7818e1d3
4 changed files with 52 additions and 8 deletions
|
@ -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())
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package grpc
|
||||
|
||||
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())
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package loop
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -10,6 +9,7 @@ import (
|
|||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/plugin/pkg/rand"
|
||||
)
|
||||
|
||||
func init() { plugin.Register("loop", setup) }
|
||||
|
@ -84,4 +84,4 @@ func qname(zone string) string {
|
|||
return dnsutil.Join(l1, l2, zone)
|
||||
}
|
||||
|
||||
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
var r = rand.New(time.Now().UnixNano())
|
||||
|
|
36
plugin/pkg/rand/rand.go
Normal file
36
plugin/pkg/rand/rand.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
// Package rand is used for concurrency safe random number generator.
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Rand is used for concurrency safe random number generator.
|
||||
type Rand struct {
|
||||
m sync.Mutex
|
||||
r *rand.Rand
|
||||
}
|
||||
|
||||
// New returns a new Rand from seed.
|
||||
func New(seed int64) *Rand {
|
||||
return &Rand{r: rand.New(rand.NewSource(seed))}
|
||||
}
|
||||
|
||||
// Int returns a non-negative pseudo-random int from the Source in Rand.r.
|
||||
func (r *Rand) Int() int {
|
||||
r.m.Lock()
|
||||
v := r.r.Int()
|
||||
r.m.Unlock()
|
||||
return v
|
||||
}
|
||||
|
||||
// Perm returns, as a slice of n ints, a pseudo-random permutation of the
|
||||
// integers in the half-open interval [0,n) from the Source in Rand.r.
|
||||
func (r *Rand) Perm(n int) []int {
|
||||
r.m.Lock()
|
||||
v := r.r.Perm(n)
|
||||
r.m.Unlock()
|
||||
return v
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue