mirror of https://github.com/knative/pkg.git
Fix rand source in name (#3070)
This commit is contained in:
parent
ee1db869c7
commit
3f6a546ac3
|
|
@ -19,6 +19,7 @@ package helpers
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
@ -32,7 +33,10 @@ const (
|
||||||
testNamePrefix = "Test"
|
testNamePrefix = "Test"
|
||||||
)
|
)
|
||||||
|
|
||||||
var nameRand *rand.Rand
|
var (
|
||||||
|
nameRand *rand.Rand
|
||||||
|
nameMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Properly seed the random number generator so RandomString() is actually random.
|
// Properly seed the random number generator so RandomString() is actually random.
|
||||||
|
|
@ -75,9 +79,11 @@ func AppendRandomString(prefix string) string {
|
||||||
// RandomString will generate a random string.
|
// RandomString will generate a random string.
|
||||||
func RandomString() string {
|
func RandomString() string {
|
||||||
suffix := make([]byte, randSuffixLen)
|
suffix := make([]byte, randSuffixLen)
|
||||||
|
nameMu.Lock()
|
||||||
for i := range suffix {
|
for i := range suffix {
|
||||||
suffix[i] = letterBytes[nameRand.Intn(len(letterBytes))]
|
suffix[i] = letterBytes[nameRand.Intn(len(letterBytes))]
|
||||||
}
|
}
|
||||||
|
nameMu.Unlock()
|
||||||
return string(suffix)
|
return string(suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue