Skip to content

Commit 435fa42

Browse files
authored
♻️ Refactor: Migrate randString to rand v2 (#3329)
* ♻️ Refactor: migrate randString to rand/v2 * 🩹 Fix: golangci-lint
1 parent 3f93555 commit 435fa42

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

client/hooks.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@ package client
33
import (
44
"fmt"
55
"io"
6-
"math/rand"
6+
"math/rand/v2"
77
"mime/multipart"
88
"os"
99
"path/filepath"
1010
"regexp"
1111
"strconv"
1212
"strings"
13-
"time"
1413

1514
"github.com/gofiber/utils/v2"
1615
"github.com/valyala/fasthttp"
1716
)
1817

19-
var (
20-
protocolCheck = regexp.MustCompile(`^https?://.*$`)
21-
22-
headerAccept = "Accept"
18+
var protocolCheck = regexp.MustCompile(`^https?://.*$`)
2319

20+
const (
21+
headerAccept = "Accept"
2422
applicationJSON = "application/json"
2523
applicationCBOR = "application/cbor"
2624
applicationXML = "application/xml"
@@ -30,25 +28,26 @@ var (
3028
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
3129
letterIdxBits = 6 // 6 bits to represent a letter index
3230
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
33-
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting into 63 bits
31+
letterIdxMax = 64 / letterIdxBits // # of letter indices fitting into 64 bits
3432
)
3533

36-
// randString returns a random string of length n.
37-
func randString(n int) string {
34+
// unsafeRandString returns a random string of length n.
35+
func unsafeRandString(n int) string {
3836
b := make([]byte, n)
39-
length := len(letterBytes)
40-
src := rand.NewSource(time.Now().UnixNano())
37+
const length = uint64(len(letterBytes))
4138

42-
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
39+
//nolint:gosec // Not a concern
40+
for i, cache, remain := n-1, rand.Uint64(), letterIdxMax; i >= 0; {
4341
if remain == 0 {
44-
cache, remain = src.Int63(), letterIdxMax
42+
//nolint:gosec // Not a concern
43+
cache, remain = rand.Uint64(), letterIdxMax
4544
}
4645

47-
if idx := int(cache & int64(letterIdxMask)); idx < length {
46+
if idx := cache & letterIdxMask; idx < length {
4847
b[i] = letterBytes[idx]
4948
i--
5049
}
51-
cache >>= int64(letterIdxBits)
50+
cache >>= letterIdxBits
5251
remain--
5352
}
5453

@@ -134,7 +133,7 @@ func parserRequestHeader(c *Client, req *Request) error {
134133
req.RawRequest.Header.SetContentType(multipartFormData)
135134
// If boundary is default, append a random string to it.
136135
if req.boundary == boundary {
137-
req.boundary += randString(16)
136+
req.boundary += unsafeRandString(16)
138137
}
139138
req.RawRequest.Header.SetMultipartFormBoundary(req.boundary)
140139
default:

client/hooks_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Test_Rand_String(t *testing.T) {
3838
for _, tt := range tests {
3939
t.Run(tt.name, func(t *testing.T) {
4040
t.Parallel()
41-
got := randString(tt.args)
41+
got := unsafeRandString(tt.args)
4242
require.Len(t, got, tt.args)
4343
})
4444
}

0 commit comments

Comments
 (0)