mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-25 18:04:58 +01:00
3cb1865909
math/rand.Read is deprecated in newer Go versions. Also, it appears that crypto/rand is faster when used from multiple goroutines: https://gist.github.com/problame/0699acd6f99db4163f26f0b8a61569f3
36 lines
630 B
Go
36 lines
630 B
Go
package trace
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"strings"
|
|
|
|
"github.com/zrepl/zrepl/util/envconst"
|
|
)
|
|
|
|
var genIdNumBytes = envconst.Int("ZREPL_TRACE_ID_NUM_BYTES", 3)
|
|
|
|
func init() {
|
|
if genIdNumBytes < 1 {
|
|
panic("trace node id byte length must be at least 1")
|
|
}
|
|
}
|
|
|
|
func genID() string {
|
|
var out strings.Builder
|
|
enc := base64.NewEncoder(base64.RawStdEncoding, &out)
|
|
buf := make([]byte, genIdNumBytes)
|
|
_, err := rand.Read(buf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
n, err := enc.Write(buf[:])
|
|
if err != nil || n != len(buf) {
|
|
panic(err)
|
|
}
|
|
if err := enc.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
return out.String()
|
|
}
|