diff --git a/changelog/fragments/1724253791-replace-math-rand-with-math-rand-v2.yaml b/changelog/fragments/1724253791-replace-math-rand-with-math-rand-v2.yaml new file mode 100644 index 00000000000..82753156b7e --- /dev/null +++ b/changelog/fragments/1724253791-replace-math-rand-with-math-rand-v2.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: feature + +# Change summary; a 80ish characters long description of the change. +summary: replace math/rand with math/rand/v2 + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +#description: + +# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. +component: all + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +pr: https://github.com/elastic/elastic-agent/pull/5336 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +issue: https://github.com/elastic/elastic-agent/issues/5335 diff --git a/internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go b/internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go index 663147ba0a3..20e44a78b69 100644 --- a/internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go +++ b/internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go @@ -7,7 +7,7 @@ package http import ( "context" "fmt" - "math/rand" + "math/rand/v2" "net/http" "net/http/httptest" "net/url" @@ -111,8 +111,8 @@ func runTests(t *testing.T, testCases []testCase, config *artifact.Config, log * func getRandomTestCases() []testCase { tt := getTestCases() - first := rand.Intn(len(tt)) - second := rand.Intn(len(tt)) + first := rand.IntN(len(tt)) + second := rand.IntN(len(tt)) return []testCase{ tt[first], diff --git a/internal/pkg/agent/application/upgrade/marker_access_test.go b/internal/pkg/agent/application/upgrade/marker_access_test.go index 21f1488ac1d..a32046552db 100644 --- a/internal/pkg/agent/application/upgrade/marker_access_test.go +++ b/internal/pkg/agent/application/upgrade/marker_access_test.go @@ -8,7 +8,7 @@ import ( "context" "errors" "fmt" - "math/rand" + "math/rand/v2" "os" "path/filepath" "sync" @@ -159,7 +159,7 @@ func randomBytes(length int) []byte { var b []byte for i := 0; i < length; i++ { - rune := chars[rand.Intn(len(chars))] + rune := chars[rand.IntN(len(chars))] b = append(b, byte(rune)) } diff --git a/internal/pkg/agent/cmd/enroll_cmd.go b/internal/pkg/agent/cmd/enroll_cmd.go index bbfa3be1f4a..6c1438cb6df 100644 --- a/internal/pkg/agent/cmd/enroll_cmd.go +++ b/internal/pkg/agent/cmd/enroll_cmd.go @@ -9,7 +9,7 @@ import ( "context" "fmt" "io" - "math/rand" + "math/rand/v2" "os" "os/exec" "strings" @@ -704,7 +704,7 @@ func yamlToReader(in interface{}) (io.Reader, error) { } func delay(ctx context.Context, d time.Duration) { - t := time.NewTimer(time.Duration(rand.Int63n(int64(d)))) + t := time.NewTimer(rand.N(d)) defer t.Stop() select { case <-ctx.Done(): diff --git a/internal/pkg/core/backoff/equal_jitter.go b/internal/pkg/core/backoff/equal_jitter.go index 671201f5892..8436af645eb 100644 --- a/internal/pkg/core/backoff/equal_jitter.go +++ b/internal/pkg/core/backoff/equal_jitter.go @@ -5,7 +5,7 @@ package backoff import ( - "math/rand" + "math/rand/v2" "time" ) @@ -30,7 +30,7 @@ func NewEqualJitterBackoff(done <-chan struct{}, init, max time.Duration) Backof done: done, init: init, max: max, - nextRand: time.Duration(rand.Int63n(int64(init))), //nolint:gosec + nextRand: time.Duration(rand.N(init)), } } @@ -51,7 +51,7 @@ func (b *EqualJitterBackoff) Wait() bool { backoff := b.NextWait() // increase duration for next wait. - b.nextRand = time.Duration(rand.Int63n(int64(b.duration))) + b.nextRand = rand.N(b.duration) b.duration *= 2 if b.duration > b.max { b.duration = b.max diff --git a/internal/pkg/remote/client.go b/internal/pkg/remote/client.go index 991b7ed55e2..50faf1cd2c7 100644 --- a/internal/pkg/remote/client.go +++ b/internal/pkg/remote/client.go @@ -9,7 +9,7 @@ import ( "errors" "fmt" "io" - "math/rand" + "math/rand/v2" "net/http" "net/url" "sort" diff --git a/internal/pkg/scheduler/scheduler.go b/internal/pkg/scheduler/scheduler.go index c4c9b9d55eb..de5ee3c4581 100644 --- a/internal/pkg/scheduler/scheduler.go +++ b/internal/pkg/scheduler/scheduler.go @@ -5,7 +5,7 @@ package scheduler import ( - "math/rand" + "math/rand/v2" "time" ) @@ -129,6 +129,5 @@ func (p *PeriodicJitter) Stop() { } func (p *PeriodicJitter) delay() time.Duration { - t := int64(p.variance) - return time.Duration(rand.Int63n(t)) + return rand.N(p.variance) } diff --git a/magefile.go b/magefile.go index 8106031c0a2..1203d319901 100644 --- a/magefile.go +++ b/magefile.go @@ -15,7 +15,7 @@ import ( "fmt" "html/template" "log" - "math/rand" + "math/rand/v2" "net/http" "os" "os/exec" diff --git a/main.go b/main.go index 98580c51c5b..de511ac3957 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,7 @@ package main import ( "fmt" - "math/rand" "os" - "time" "github.com/elastic/elastic-agent/internal/pkg/agent/cmd" "github.com/elastic/elastic-agent/pkg/core/process" @@ -36,7 +34,6 @@ func main() { } defer pj.Close() - rand.Seed(time.Now().UnixNano()) command := cmd.NewCommand() err = command.Execute() if err != nil { diff --git a/testing/integration/install_test.go b/testing/integration/install_test.go index f2159752943..f9fae0046fc 100644 --- a/testing/integration/install_test.go +++ b/testing/integration/install_test.go @@ -9,7 +9,7 @@ package integration import ( "context" "fmt" - "math/rand" + "math/rand/v2" "os" "path/filepath" "runtime" @@ -334,12 +334,11 @@ func TestRepeatedInstallUninstall(t *testing.T) { } func randStr(length int) string { - rand.Seed(time.Now().UnixNano()) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") runes := make([]rune, length) for i := range runes { - runes[i] = letters[rand.Intn(len(letters))] + runes[i] = letters[rand.IntN(len(letters))] } return string(runes)