Skip to content

Commit

Permalink
replace math/rand with math/rand/v2 (#5336)
Browse files Browse the repository at this point in the history
* chore: use math/rand/v2 instead of math/rand

* remove unnecessary conversion

* remove unused changelog fragment

---------

Co-authored-by: VihasMakwana <[email protected]>
  • Loading branch information
mauri870 and VihasMakwana authored Aug 27, 2024
1 parent dd397ae commit 6bb6b1e
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package http
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/agent/application/upgrade/marker_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -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))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -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():
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/core/backoff/equal_jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package backoff

import (
"math/rand"
"math/rand/v2"
"time"
)

Expand All @@ -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: rand.N(init),
}
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net/http"
"net/url"
"sort"
Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package scheduler

import (
"math/rand"
"math/rand/v2"
"time"
)

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"fmt"
"html/template"
"log"
"math/rand"
"math/rand/v2"
"net/http"
"os"
"os/exec"
Expand Down
4 changes: 2 additions & 2 deletions testing/integration/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package integration
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -338,7 +338,7 @@ func randStr(length int) string {

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)
Expand Down

0 comments on commit 6bb6b1e

Please sign in to comment.