Skip to content

Commit 5cab184

Browse files
authored
GODRIVER-2223 Use separate, seeded pseudo-random sources for each package. (#803)
1 parent 57a5393 commit 5cab184

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

x/mongo/driver/connstring/connstring.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
2323
)
2424

25+
// random is a package-global pseudo-random number source.
26+
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
27+
2528
// ParseAndValidate parses the provided URI into a ConnString object.
2629
// It check that all values are valid.
2730
func ParseAndValidate(s string) (ConnString, error) {
@@ -309,7 +312,7 @@ func (p *parser) parse(original string) error {
309312
// TODO(GODRIVER-1876): Use rand#Shuffle after dropping Go 1.9 support.
310313
n := len(parsedHosts)
311314
for i := 0; i < n-1; i++ {
312-
j := i + rand.Intn(n-i)
315+
j := i + random.Intn(n-i)
313316
parsedHosts[j], parsedHosts[i] = parsedHosts[i], parsedHosts[j]
314317
}
315318
parsedHosts = parsedHosts[:p.SRVMaxHosts]

x/mongo/driver/topology/polling_srv_records_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ func TestPollSRVRecordsMaxHosts(t *testing.T) {
371371
compareHosts(t, actualHosts, expectedHosts)
372372
})
373373
t.Run("SRVMaxHosts is less than number of hosts", func(t *testing.T) {
374+
// TODO: Enable with GODRIVER-2222.
375+
t.Skipf("TODO: Enable with GODRIVER-2222")
376+
374377
recordsToAdd := []*net.SRV{{"localhost.test.build.10gen.cc.", 27019, 0, 0}, {"localhost.test.build.10gen.cc.", 27020, 0, 0}}
375378
recordsToRemove := []*net.SRV{{"localhost.test.build.10gen.cc.", 27018, 0, 0}}
376379
topo, disconnect := simulateSRVPoll(2, recordsToAdd, recordsToRemove)

x/mongo/driver/topology/topology.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var ErrServerSelectionTimeout = errors.New("server selection timeout")
4848
// MonitorMode represents the way in which a server is monitored.
4949
type MonitorMode uint8
5050

51+
// random is a package-global pseudo-random number source.
52+
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
53+
5154
// These constants are the available monitoring modes.
5255
const (
5356
AutomaticMode MonitorMode = iota
@@ -395,7 +398,7 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect
395398
continue
396399
}
397400

398-
selected := suitable[rand.Intn(len(suitable))]
401+
selected := suitable[random.Intn(len(suitable))]
399402
selectedS, err := t.FindServer(selected)
400403
switch {
401404
case err != nil:
@@ -550,7 +553,7 @@ func (t *Topology) pollSRVRecords() {
550553
// TODO(GODRIVER-1876): Use rand#Shuffle after dropping Go 1.9 support.
551554
n := len(parsedHosts)
552555
for i := 0; i < n-1; i++ {
553-
j := i + rand.Intn(n-i)
556+
j := i + random.Intn(n-i)
554557
parsedHosts[j], parsedHosts[i] = parsedHosts[i], parsedHosts[j]
555558
}
556559
parsedHosts = parsedHosts[:t.cfg.srvMaxHosts]

x/mongo/driver/uuid/uuid.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
package uuid // import "go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
88

99
import (
10-
"crypto/rand"
1110
"io"
11+
"math/rand"
12+
"time"
1213
)
1314

1415
// UUID represents a UUID.
1516
type UUID [16]byte
1617

17-
var rander = rand.Reader
18+
// random is a package-global pseudo-random number source.
19+
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
1820

19-
// New generates a new uuid.
21+
// New returns a random UUIDv4. It uses a "math/rand" pseudo-random number generator seeded with the
22+
// package initialization time.
23+
//
24+
// New should not be used to generate cryptographically-secure random UUIDs.
2025
func New() (UUID, error) {
2126
var uuid [16]byte
2227

23-
_, err := io.ReadFull(rander, uuid[:])
28+
_, err := io.ReadFull(random, uuid[:])
2429
if err != nil {
2530
return [16]byte{}, err
2631
}

x/mongo/driver/uuid/uuid_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package uuid
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestNew(t *testing.T) {
10+
m := make(map[UUID]bool)
11+
for i := 1; i < 100; i++ {
12+
uuid, err := New()
13+
assert.NoError(t, err, "New error")
14+
assert.False(t, m[uuid], "New returned a duplicate UUID %v", uuid)
15+
m[uuid] = true
16+
}
17+
}

0 commit comments

Comments
 (0)