Commit 4ec6e4e 1 parent 8299ab9 commit 4ec6e4e Copy full SHA for 4ec6e4e
File tree 3 files changed +9
-7
lines changed
3 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -17,12 +17,14 @@ import (
17
17
//===========================================================================
18
18
19
19
var defaultEntropy = func () io.Reader {
20
- rng := rand .New (rand .NewSource (time .Now ().UnixNano ()))
21
- return & LockedMonotonicReader {MonotonicReader : Monotonic (rng , 0 )}
20
+ return Pool (func () io.Reader {
21
+ return Monotonic (rand .New (rand .NewSource (time .Now ().UnixNano ())), 0 )
22
+ })
22
23
}()
23
24
24
25
// DefaultEntropy returns a thread-safe per process monotonically increasing
25
- // entropy source.
26
+ // entropy source. It uses a sync.Pool rather than a sync.Mutex to provide
27
+ // minimal contention for concurrent access.
26
28
func DefaultEntropy () io.Reader {
27
29
return defaultEntropy
28
30
}
@@ -31,9 +33,7 @@ func DefaultEntropy() io.Reader {
31
33
// Secure Entropy
32
34
//===========================================================================
33
35
34
- var secureEntropy = func () io.Reader {
35
- return Pool (func () io.Reader { return crand .Reader })
36
- }()
36
+ var secureEntropy = crand .Reader
37
37
38
38
// SecureEntropy returns a thread-safe per process monotonically increasing
39
39
// entropy source that uses cryptographically random generation and a sync.Pool
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ func MustNewSecure(t time.Time) ULID {
110
110
111
111
// Make returns a ULID with the current time in Unix milliseconds and
112
112
// monotonically increasing entropy for the same millisecond.
113
- // It is safe for concurrent use, using a sync.Mutex to protect entropy access .
113
+ // It is safe for concurrent use, using a sync.Pool to minimize contention .
114
114
func Make () (id ULID ) {
115
115
// NOTE: MustNew can't panic since DefaultEntropy never returns an error.
116
116
return MustNew (Now (), defaultEntropy )
Original file line number Diff line number Diff line change @@ -368,6 +368,8 @@ func TestParseRobustness(t *testing.T) {
368
368
}
369
369
370
370
func TestParseTypes (t * testing.T ) {
371
+ t .Parallel ()
372
+
371
373
example := ulid .Make ()
372
374
testCases := []struct {
373
375
input any
You can’t perform that action at this time.
0 commit comments