oklog / ulid

Universally Unique Lexicographically Sortable Identifier (ULID) in Go
Apache License 2.0
4.44k stars 161 forks source link

Is this a safe way to use ulid concurrently with other libraries too? #79

Closed frederikhors closed 2 years ago

frederikhors commented 2 years ago

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 the math package is not safe for concurrent use. Instantiate one per long living go-routine or use a sync.Pool if you want to avoid the potential contention of a locked rand.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?

tsenart commented 2 years ago

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.

tsenart commented 2 years ago

I opened https://github.com/oklog/ulid/pull/80 to improve the README.

tsenart commented 2 years ago

As a follow up, here's a proposal for a ulid.Make() function that has sane defaults and is safe for concurrent use: https://github.com/oklog/ulid/pull/81

frederikhors commented 2 years ago

The method described here is really fast! Can we use it with ulid?

peterbourgon commented 2 years ago

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.

frederikhors commented 2 years ago

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?

tsenart commented 2 years ago

Merged #81