feat: 实现并发安全的 ULID trace ID 生成器

This commit is contained in:
hhs
2026-06-21 21:59:43 +08:00
parent 11c3955bd6
commit e0dc8272a5

View File

@@ -0,0 +1,22 @@
package trace
import (
cryptorand "crypto/rand"
"sync"
"time"
"github.com/oklog/ulid/v2"
)
var entropyPool = sync.Pool{
New: func() interface{} {
return ulid.Monotonic(cryptorand.Reader, 0)
},
}
// GenerateTraceID 生成并发安全的 ULID trace ID
func GenerateTraceID() string {
entropy := entropyPool.Get().(*ulid.MonotonicEntropy)
defer entropyPool.Put(entropy)
return ulid.MustNew(ulid.Timestamp(time.Now()), entropy).String()
}