Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tracer/util.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package tracer

import (
cryptorand "crypto/rand"
"encoding/binary"
"math/rand"
"sync"
"time"
)

var (
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
seededIDGen = rand.New(rand.NewSource(generateSeed()))
// The golang rand generators are *not* intrinsically thread-safe.
seededIDLock sync.Mutex
)

func generateSeed() int64 {
var b [8]byte
_, err := cryptorand.Read(b[:])
if err != nil {
// Cannot seed math/rand package with cryptographically secure random number generator
// Fallback to time.Now()
return time.Now().UnixNano()
}

return int64(binary.LittleEndian.Uint64(b[:]))
}

func randomID() uint64 {
seededIDLock.Lock()
defer seededIDLock.Unlock()
Expand Down