1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00

Update to math/rand/v2

This commit updates the uses of math/rand to math/rand/v2, which was
released in Go 1.22 (2024-02).

The new package is generally safer, see https://go.dev/blog/randv2 for
the details.

There are no user-visible changes, it is only adjusting the name of
functions, simplify some code thanks to v2 having a better API, etc.
This commit is contained in:
Alberto Bertogli
2025-02-16 20:18:16 +00:00
parent cef7bb079d
commit 45580dae46
7 changed files with 22 additions and 31 deletions

View File

@@ -9,8 +9,9 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"math/rand"
"math/rand/v2"
"os"
"os/exec"
"path/filepath"
@@ -67,17 +68,11 @@ var newID chan string
func generateNewIDs() {
// The IDs are only used internally, we are ok with using a PRNG.
// We create our own to avoid relying on external sources initializing it
// properly.
prng := rand.New(rand.NewSource(time.Now().UnixNano()))
// IDs are base64(8 random bytes), but the code doesn't care.
buf := make([]byte, 8)
id := ""
for {
prng.Read(buf)
id = base64.RawURLEncoding.EncodeToString(buf)
newID <- id
binary.NativeEndian.PutUint64(buf, rand.Uint64())
newID <- base64.RawURLEncoding.EncodeToString(buf)
}
}
@@ -485,7 +480,7 @@ func nextDelay(createdAt time.Time) time.Duration {
// Perturb the delay, to avoid all queued emails to be retried at the
// exact same time after a restart.
delay += time.Duration(rand.Intn(60)) * time.Second
delay += rand.N(60 * time.Second)
return delay
}