chore: use math/rand/v2

This commit is contained in:
Fernandez Ludovic 2026-03-05 11:37:51 +01:00
commit ec2670e105
No known key found for this signature in database
GPG key ID: 581FF80B781EF668
6 changed files with 12 additions and 13 deletions

View file

@ -4,7 +4,7 @@ import (
"context"
"crypto/x509"
"fmt"
"math/rand"
"math/rand/v2"
"time"
"github.com/go-acme/lego/v5/acme"
@ -31,7 +31,7 @@ func (r *RenewalInfo) ShouldRenewAt(now time.Time, willingToSleep time.Duration)
// Select a uniform random time within the suggested window.
rt := start
if window := end.Sub(start); window > 0 {
randomDuration := time.Duration(rand.Int63n(int64(window)))
randomDuration := time.Duration(rand.Int64N(int64(window)))
rt = rt.Add(randomDuration)
}

View file

@ -7,7 +7,7 @@ import (
"fmt"
"log/slog"
"math"
"math/rand"
"math/rand/v2"
"os"
"slices"
"sort"
@ -391,8 +391,7 @@ func randomSleep(cmd *cli.Command) {
// https://github.com/certbot/certbot/blob/284023a1b7672be2bd4018dd7623b3b92197d4b0/certbot/certbot/_internal/renewal.py#L472
const jitter = 8 * time.Minute
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
sleepTime := time.Duration(rnd.Int63n(int64(jitter)))
sleepTime := time.Duration(rand.Int64N(int64(jitter)))
log.Info("renewal: random delay.", slog.Duration("sleep", sleepTime))
time.Sleep(sleepTime)

View file

@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"strconv"
"time"
@ -98,7 +98,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
options.Backoff = retry.BackoffDelayerFunc(func(attempt int, err error) (time.Duration, error) {
retryCount := min(attempt, 7)
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
delay := (1 << uint(retryCount)) * (rand.IntN(50) + 200)
return time.Duration(delay) * time.Millisecond, nil
})

View file

@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"testing"
@ -63,7 +63,7 @@ func mockAPICreate(recs map[int]network.DNSRecord) http.HandlerFunc {
return
}
payload.Params.ID = types.FlexInt(rand.Intn(10000000))
payload.Params.ID = types.FlexInt(rand.IntN(10000000))
payload.Params.ZoneID = types.FlexInt(mockAPIServerZones[payload.Params.Name])
if _, exists := recs[int(payload.Params.ID)]; exists {

View file

@ -6,7 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net/http"
"net/url"
"strconv"
@ -136,7 +136,7 @@ func getRandomSalt() []byte {
// This is the only part of this that needs to be serialized.
salt := make([]byte, 16)
for i := range 16 {
salt[i] = saltBytes[rand.Intn(len(saltBytes))]
salt[i] = saltBytes[rand.IntN(len(saltBytes))]
}
return salt

View file

@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"strings"
"time"
@ -354,7 +354,7 @@ func createAWSConfig(ctx context.Context, config *Config) (aws.Config, error) {
options.Backoff = retry.BackoffDelayerFunc(func(attempt int, err error) (time.Duration, error) {
retryCount := min(attempt, 7)
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
delay := (1 << uint(retryCount)) * (rand.IntN(50) + 200)
return time.Duration(delay) * time.Millisecond, nil
})