From 3cb1865909fb3de00359c27186ce5dc86b921c6e Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 8 Sep 2024 15:34:39 +0000 Subject: [PATCH] chore: trace spans: use crypto/rand for generating them 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 --- daemon/daemon.go | 7 ------- daemon/logging/trace/trace_genID.go | 11 ++++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/daemon/daemon.go b/daemon/daemon.go index 75333e0..f83b758 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -3,7 +3,6 @@ package daemon import ( "context" "fmt" - "math/rand" "os" "os/signal" "strings" @@ -39,12 +38,6 @@ func Run(ctx context.Context, conf *config.Config) error { cancel() }() - // The math/rand package is used presently for generating trace IDs, we - // seed it with the current time and pid so that the IDs are mostly - // unique. - rand.Seed(time.Now().UnixNano()) - rand.Seed(int64(os.Getpid())) - outlets, err := logging.OutletsFromConfig(*conf.Global.Logging) if err != nil { return errors.Wrap(err, "cannot build logging from config") diff --git a/daemon/logging/trace/trace_genID.go b/daemon/logging/trace/trace_genID.go index 908e3bd..f8ac7bf 100644 --- a/daemon/logging/trace/trace_genID.go +++ b/daemon/logging/trace/trace_genID.go @@ -1,8 +1,8 @@ package trace import ( + "crypto/rand" "encoding/base64" - "math/rand" "strings" "github.com/zrepl/zrepl/util/envconst" @@ -20,12 +20,9 @@ func genID() string { var out strings.Builder enc := base64.NewEncoder(base64.RawStdEncoding, &out) buf := make([]byte, genIdNumBytes) - for i := 0; i < len(buf); { - n, err := rand.Read(buf[i:]) - if err != nil { - panic(err) - } - i += n + _, err := rand.Read(buf) + if err != nil { + panic(err) } n, err := enc.Write(buf[:]) if err != nil || n != len(buf) {