forked from lightstep/lightstep-tracer-go
/
util.go
36 lines (30 loc) · 849 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package lightstep
import (
"math/rand"
"sync"
"time"
)
var (
seededGUIDGen *rand.Rand
seededGUIDGenOnce sync.Once
seededGUIDLock sync.Mutex
)
func genSeededGUID() uint64 {
// Golang does not seed the rng for us. Make sure it happens.
seededGUIDGenOnce.Do(func() {
seededGUIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
})
// The golang random generators are *not* intrinsically thread-safe.
seededGUIDLock.Lock()
defer seededGUIDLock.Unlock()
return uint64(seededGUIDGen.Int63())
}
func genSeededGUID2() (uint64, uint64) {
// Golang does not seed the rng for us. Make sure it happens.
seededGUIDGenOnce.Do(func() {
seededGUIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
})
seededGUIDLock.Lock()
defer seededGUIDLock.Unlock()
return uint64(seededGUIDGen.Int63()), uint64(seededGUIDGen.Int63())
}