Skip to content

Commit

Permalink
all: use global rand to seed random generators
Browse files Browse the repository at this point in the history
Global random generator is automatically seeded since go1.20,
see https://go.dev/doc/go1.20#math/rand and golang/go#54880

This change uses global generator to seed random sources instead of time.Now().UnixNano().

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov committed Jul 2, 2023
1 parent 969bccd commit 3fcb730
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion filters/auth/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ func parseFilter(def, oidcServerURL, redirectURL string) ([]*eskip.Filter, error
}

func TestChunkAndMergerCookie(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(rand.Int63()))
tinyCookie := http.Cookie{
Name: "skipperOauthOidcHASHHASH-",
Value: "eyJ0eXAiOiJKV1QiLCJhbGciO",
Expand Down
2 changes: 1 addition & 1 deletion filters/builtin/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type errorReader struct {
var testContent []byte

func init() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(rand.Int63()))
testContent = make([]byte, maxTestContent)
n, err := r.Read(testContent)

Expand Down
2 changes: 1 addition & 1 deletion filters/diag/absorb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func testAbsorb(t *testing.T, silent bool) {
"POST",
p.URL,
io.LimitReader(
rand.New(rand.NewSource(time.Now().UnixNano())),
rand.New(rand.NewSource(rand.Int63())),
bodySize,
),
)
Expand Down
2 changes: 1 addition & 1 deletion filters/diag/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (r *random) CreateFilter(args []interface{}) (filters.Filter, error) {
}

if l, ok := args[0].(float64); ok {
return &random{rand: rand.New(rand.NewSource(time.Now().UnixNano())), len: int64(l)}, nil // #nosec
return &random{rand: rand.New(rand.NewSource(rand.Int63())), len: int64(l)}, nil // #nosec
} else {
return nil, filters.ErrInvalidFilterParameters
}
Expand Down
3 changes: 1 addition & 2 deletions filters/flowid/ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/rand"
"regexp"
"sync"
"time"

"github.com/oklog/ulid"
)
Expand All @@ -26,7 +25,7 @@ var ulidFlowIDRegex = regexp.MustCompile(`^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26
// It uses a shared, pseudo-random source of entropy, seeded with the current timestamp.
// It is safe for concurrent usage.
func NewULIDGenerator() Generator {
return NewULIDGeneratorWithEntropyProvider(rand.New(rand.NewSource(time.Now().UTC().UnixNano()))) // #nosec
return NewULIDGeneratorWithEntropyProvider(rand.New(rand.NewSource(rand.Int63()))) // #nosec
}

// NewULIDGeneratorWithEntropyProvider behaves like NewULIDGenerator but allows you to specify your own source of
Expand Down
4 changes: 2 additions & 2 deletions loadbalancer/fadein_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func testFadeIn(
})
}

rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := rand.New(rand.NewSource(rand.Int63()))
t.Log("test start", time.Now())
var stats []string
stop := time.After(fadeInDuration)
Expand Down Expand Up @@ -232,7 +232,7 @@ func benchmarkFadeIn(
go func(i int) {
defer wg.Done()

rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := rand.New(rand.NewSource(rand.Int63()))
ctx := &routing.LBContext{
Params: map[string]interface{}{},
Route: route,
Expand Down
3 changes: 1 addition & 2 deletions loadbalancer/locked_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package loadbalancer
import (
"math/rand"
"sync"
"time"
)

type lockedSource struct {
Expand All @@ -12,7 +11,7 @@ type lockedSource struct {
}

func newLockedSource() *lockedSource {
return &lockedSource{r: rand.NewSource(time.Now().UnixNano())}
return &lockedSource{r: rand.NewSource(rand.Int63())} // #nosec
}

func (s *lockedSource) Int63() int64 {
Expand Down
3 changes: 1 addition & 2 deletions predicates/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/rand"
"net/http"
"testing"
"time"

"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
Expand Down Expand Up @@ -314,7 +313,7 @@ func TestMatchingClientIP(t *testing.T) {
}
}

var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var random = rand.New(rand.NewSource(rand.Int63()))

func generateIPCidr() string {
if m := random.Int(); m%10 == 0 {
Expand Down
2 changes: 1 addition & 1 deletion proxy/idle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func hasArg(arg string) bool {
// This test is unpredictable, and occasionally fails on certain OSes.
// To run this test, set `-args idle` for the test command.
func TestIdleConns(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(rand.Int63()))
if !hasArg("idle") {
t.Skip()
}
Expand Down

0 comments on commit 3fcb730

Please sign in to comment.