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

662 Views Asked by At

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?

1

There are 1 best solutions below

5
Wagner Riffel On

Is this a safe way to use the package?

No, that sentence suggests that each rand.Source should be local to the goroutine, your defaultEntropySource rand.Source piece is potentially shared between multiple goroutines.

As documentated New function, you only need to make sure the entropy reader is safe for concurrent use, but Monotonic is not. Here is a two ways of implementing the documentation suggestion:

Create a single rand.Source per call o NewID(), allocates a new entropy for each call to NewID

func NewID() string {
    defaultEntropySource := ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
    return ulid.MustNew(ulid.Timestamp(time.Now()), defaultEntropySource).String()
}

Playground

Like above but using sync.Pool to possible reuse previously allocated rand.Sources

var entropyPool = sync.Pool{
    New: func() any {
        entropy := ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
        return entropy
    },
}

func NewID() string {
    e := entropyPool.Get().(*ulid.MonotonicEntropy)
    s := ulid.MustNew(ulid.Timestamp(time.Now()), e).String()
    entropyPool.Put(e)
    return s
}

Playground