Skip to content

Commit 7398264

Browse files
committed
Use math/rand/v2 in favor of dhrand
As of Go 1.20, math/rand randomly seeds during runtime - https://go.dev/doc/go1.20#mathrandpkgmathrand math/rand/v2 has a better API - https://go.dev/blog/randv2
1 parent 1db2fe1 commit 7398264

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/dhui/satomic
33
require (
44
github.com/DATA-DOG/go-sqlmock v1.4.0
55
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
6-
github.com/dhui/dhrand v0.1.0
76
github.com/dhui/dktest v0.4.5
87
github.com/go-sql-driver/mysql v1.4.1
98
github.com/jmoiron/sqlx v1.2.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1515
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1616
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73 h1:OGNva6WhsKst5OZf7eZOklDztV3hwtTHovdrLHV+MsA=
1717
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
18-
github.com/dhui/dhrand v0.1.0 h1:C8ES1tf4bO37VmLmjdcOR03nYVQDJuF26mMyUITz4NY=
19-
github.com/dhui/dhrand v0.1.0/go.mod h1:tK3W7Gn/9L6SqpDX5QmS0gX55BjYjUZbJsavMWxj5A0=
2018
github.com/dhui/dktest v0.4.5 h1:uUfYBIVREmj/Rw6MvgmqNAYzTiKOHJak+enB5Di73MM=
2119
github.com/dhui/dktest v0.4.5/go.mod h1:tmcyeHDKagvlDrz7gDKq4UAJOLIfVZYkfD5OnHDwcCo=
2220
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
@@ -79,7 +77,6 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0
7977
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
8078
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
8179
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
82-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
8380
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8481
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8582
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=

savepointers/savepoint_name.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@ package savepointers
22

33
import (
44
"encoding/base64"
5-
"math/rand"
6-
)
7-
8-
import (
9-
"github.com/dhui/dhrand"
5+
"encoding/binary"
6+
"math/rand/v2"
107
)
118

129
// number of bytes used to generate a random savepoint name. 16 bytes is plenty since it's the same size as a uuid
1310
const savepointNumBytes = 16
1411

15-
var _rand *rand.Rand
16-
17-
func init() {
18-
seed, err := dhrand.GenSeed()
19-
if err != nil {
20-
panic(err)
21-
}
22-
_rand = rand.New(dhrand.NewLockedSource(seed)) //nolint:gosec
23-
}
24-
2512
// GenSavepointName quickly generates a unique savepoint name
2613
func GenSavepointName() string {
2714
b := make([]byte, savepointNumBytes)
28-
// rand.Read() always returns len(p) and a nil err, so it's save to ignore the error.
29-
// https://golang.org/pkg/math/rand/#Rand.Read
30-
_rand.Read(b)
15+
16+
// 2 64-bit ints is 16 bytes
17+
binary.NativeEndian.PutUint64(b, rand.Uint64())
18+
binary.NativeEndian.PutUint64(b[8:], rand.Uint64())
19+
3120
return base64.RawStdEncoding.EncodeToString(b)
3221
}

0 commit comments

Comments
 (0)