Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: don't use global PRNG state from math/rand #10575

Merged
merged 1 commit into from Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions daemon/status.go
Expand Up @@ -50,9 +50,7 @@ const (
k8sMinimumEventHearbeat = time.Minute
)

func init() {
rand.Seed(time.Now().UnixNano())
}
var randGen = rand.New(rand.NewSource(time.Now().UnixNano()))

type k8sVersion struct {
version string
Expand Down Expand Up @@ -288,7 +286,7 @@ func (h *getNodes) Handle(params GetClusterNodesParams) middleware.Responder {
if exists {
clientID = *params.ClientID
} else {
clientID = rand.Int63()
clientID = randGen.Int63()
// make sure we haven't allocated an existing client ID nor the
// randomizer has allocated ID 0, if we have then we will return
// clientID 0.
Expand Down
7 changes: 3 additions & 4 deletions daemon/status_test.go
Expand Up @@ -17,7 +17,6 @@
package main

import (
"math/rand"
"time"

"github.com/cilium/cilium/api/v1/models"
Expand Down Expand Up @@ -55,12 +54,12 @@ func (g *GetNodesSuite) SetUpSuite(c *C) {
func (g *GetNodesSuite) Test_getNodesHandle(c *C) {
// Set seed so we can have the same pseudorandom client IDs.
// The seed is set to 0 for each unit test.
rand.Seed(0)
randGen.Seed(0)
const numberOfClients = 10

clientIDs := make([]int64, 0, numberOfClients)
for i := 0; i < numberOfClients; i++ {
clientIDs = append(clientIDs, rand.Int63())
clientIDs = append(clientIDs, randGen.Int63())
}

var zero int64
Expand Down Expand Up @@ -371,7 +370,7 @@ func (g *GetNodesSuite) Test_getNodesHandle(c *C) {

for _, tt := range tests {
c.Log(tt.name)
rand.Seed(0)
randGen.Seed(0)
args := tt.setupArgs()
want := tt.setupWanted()
h := &getNodes{
Expand Down
5 changes: 0 additions & 5 deletions pkg/fqdn/lookup.go
Expand Up @@ -17,17 +17,12 @@ package fqdn
import (
"fmt"
"math"
"math/rand"
"net"
"time"

"github.com/miekg/dns"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// DNSIPRecords mimics the RR data from an A or AAAA response.
// My kingdom for a DNS IP RR type that isn't hidden in the stdlib or has a
// million layers of type indirection.
Expand Down
8 changes: 3 additions & 5 deletions pkg/kvstore/etcd.go
Expand Up @@ -64,11 +64,9 @@ var (
// ErrLockLeaseExpired is an error whenever the lease of the lock does not
// exist or it was expired.
ErrLockLeaseExpired = errors.New("transaction did not succeed: lock lease expired")
)

func init() {
rand.Seed(time.Now().UnixNano())
}
randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
)

type etcdModule struct {
opts backendOptions
Expand Down Expand Up @@ -362,7 +360,7 @@ func (e *etcdClient) waitForInitLock(ctx context.Context) <-chan bool {

// Generate a random number so that we can acquire a lock even
// if other agents are killed while locking this path.
randNumber := strconv.FormatUint(rand.Uint64(), 16)
randNumber := strconv.FormatUint(randGen.Uint64(), 16)
locker, err := e.LockPath(ctx, InitLockPath+"/"+randNumber)
if err == nil {
locker.Unlock(context.Background())
Expand Down
6 changes: 3 additions & 3 deletions pkg/lock/stoppable_waitgroup_test.go
Expand Up @@ -167,13 +167,13 @@ func (s *SemaphoredMutexSuite) TestWaitChannel(c *C) {
func (s *SemaphoredMutexSuite) TestParallelism(c *C) {
l := NewStoppableWaitGroup()

rand.Seed(time.Now().Unix())
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
in := make(chan int)
stop := make(chan struct{})
go func() {
for {
select {
case in <- rand.Intn(1 - 0):
case in <- randGen.Intn(1 - 0):
case <-stop:
close(in)
return
Expand All @@ -198,7 +198,7 @@ func (s *SemaphoredMutexSuite) TestParallelism(c *C) {
}()
}

time.Sleep(time.Duration(rand.Intn(3-0)) * time.Second)
time.Sleep(time.Duration(randGen.Intn(3-0)) * time.Second)
close(stop)
wg.Wait()
add := atomic.LoadInt64(&adds)
Expand Down
11 changes: 6 additions & 5 deletions pkg/testutils/rand_name.go
Expand Up @@ -21,11 +21,12 @@ import (

// Stolen from:
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func init() {
rand.Seed(time.Now().UnixNano())
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var (
randGen = rand.New(rand.NewSource(time.Now().UnixNano()))

letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)

// RandomRuneWithPrefix returns a random name string with fixed prefix
func RandomRuneWithPrefix(prefix string, n int) string {
Expand All @@ -36,7 +37,7 @@ func RandomRuneWithPrefix(prefix string, n int) string {
func RandomRuneWithLen(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
b[i] = letterRunes[randGen.Intn(len(letterRunes))]
}
return string(b)
}
Expand Down
8 changes: 3 additions & 5 deletions test/helpers/utils.go
Expand Up @@ -38,10 +38,8 @@ import (
"golang.org/x/sys/unix"
)

func init() {
// ensure that our random numbers are seeded differently on each run
rand.Seed(time.Now().UnixNano())
}
// ensure that our random numbers are seeded differently on each run
var randGen = rand.New(rand.NewSource(time.Now().UnixNano()))

// IsRunningOnJenkins detects if the currently running Ginkgo application is
// most likely running in a Jenkins environment. Returns true if certain
Expand Down Expand Up @@ -81,7 +79,7 @@ func CountValues(key string, data []string) (int, int) {

// MakeUID returns a randomly generated string.
func MakeUID() string {
return fmt.Sprintf("%08x", rand.Uint32())
return fmt.Sprintf("%08x", randGen.Uint32())
}

// RenderTemplateToFile renders a text/template string into a target filename
Expand Down
6 changes: 2 additions & 4 deletions test/runtime/monitor.go
Expand Up @@ -27,10 +27,8 @@ import (
. "github.com/onsi/gomega"
)

func init() {
// ensure that our random numbers are seeded differently on each run
rand.Seed(time.Now().UnixNano())
}
// ensure that our random numbers are seeded differently on each run
var randGen = rand.New(rand.NewSource(time.Now().UnixNano()))

const (
// MonitorDropNotification represents the DropNotification configuration
Expand Down