Skip to content

Commit 4ec6e4e

Browse files
committed
fix pool
1 parent 8299ab9 commit 4ec6e4e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

entropy.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import (
1717
//===========================================================================
1818

1919
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+
})
2223
}()
2324

2425
// 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.
2628
func DefaultEntropy() io.Reader {
2729
return defaultEntropy
2830
}
@@ -31,9 +33,7 @@ func DefaultEntropy() io.Reader {
3133
// Secure Entropy
3234
//===========================================================================
3335

34-
var secureEntropy = func() io.Reader {
35-
return Pool(func() io.Reader { return crand.Reader })
36-
}()
36+
var secureEntropy = crand.Reader
3737

3838
// SecureEntropy returns a thread-safe per process monotonically increasing
3939
// entropy source that uses cryptographically random generation and a sync.Pool

ulid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func MustNewSecure(t time.Time) ULID {
110110

111111
// Make returns a ULID with the current time in Unix milliseconds and
112112
// 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.
114114
func Make() (id ULID) {
115115
// NOTE: MustNew can't panic since DefaultEntropy never returns an error.
116116
return MustNew(Now(), defaultEntropy)

ulid_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ func TestParseRobustness(t *testing.T) {
368368
}
369369

370370
func TestParseTypes(t *testing.T) {
371+
t.Parallel()
372+
371373
example := ulid.Make()
372374
testCases := []struct {
373375
input any

0 commit comments

Comments
 (0)