Is this a safe way to use ulid concurrently with other libraries too?
frederikhors opened this issue · comments
I just asked this question on SO. I'm posting this here too both for me and for for future "researchers".
I'm trying to use for the first time the ulid package.
In their README they say:
Please note that
rand.Rand
from themath
package is not safe for concurrent use.
Instantiate one per long living go-routine or use async.Pool
if you want to avoid the potential contention of a lockedrand.Source
as its been frequently observed in the package level functions.
Can you help me understand what does this mean and how to write SAFE code for concurrent use with libraries such ent or gqlgen?
Example: I'm using the below code in my app to generate new IDs (sometimes even many of them in the same millisecond which is fine for ulid
).
import (
"math/rand"
"time"
"github.com/oklog/ulid/v2"
)
var defaultEntropySource *ulid.MonotonicEntropy
func init() {
defaultEntropySource = ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
}
func NewID() string {
return ulid.MustNew(ulid.Timestamp(time.Now()), defaultEntropySource).String()
}
Is this a safe way to use the package?
Can you help me better understand?
Can you help me understand what does this mean and how to write SAFE code for concurrent use with libraries such ent or gqlgen?
Here are a few links relevant to the topic of thread safe random number generation in Go:
If you don't care that there is contention on a locked random number generator across go-routines, use this: https://pkg.go.dev/golang.org/x/exp/rand#example-LockedSource. I will update the README with this recommendation, since it seems most users of ulid
don't want to define their own thread safe entropy source, nor manage a sync.Pool
.
If you find there's too much contention in your program in production (via profiling with pprof
), you can implement the sync.Pool
solution.
I opened #80 to improve the README.
As a follow up, here's a proposal for a ulid.Make()
function that has sane defaults and is safe for concurrent use: #81
The method described here is really fast! Can we use it with ulid?
The method described here is really fast! Can we use it with ulid?
Of course. Just create an io.Reader that uses this as a source of entropy.
The method described here is really fast! Can we use it with ulid?
Of course. Just create an io.Reader that uses this as a source of entropy.
Can you show me an example please?
Merged #81