mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-28 03:14:57 +01:00
daemon: avoid math/rand race by using global source
Unless we're using the global source for math/rand, (*rand.Rand).Read should not be called concurrently. We seed the rng in daemon.Run to avoid ambiguity or hiding global side effects inside packages. closes #414
This commit is contained in:
parent
596a39c0f5
commit
6ac537210b
@ -3,6 +3,7 @@ package daemon
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
@ -38,6 +39,12 @@ func Run(ctx context.Context, conf *config.Config) error {
|
|||||||
cancel()
|
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)
|
outlets, err := logging.OutletsFromConfig(*conf.Global.Logging)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "cannot build logging from config")
|
return errors.Wrap(err, "cannot build logging from config")
|
||||||
|
@ -3,20 +3,11 @@ package trace
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zrepl/zrepl/util/envconst"
|
"github.com/zrepl/zrepl/util/envconst"
|
||||||
)
|
)
|
||||||
|
|
||||||
var genIdPRNG = rand.New(rand.NewSource(1))
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
genIdPRNG.Seed(time.Now().UnixNano())
|
|
||||||
genIdPRNG.Seed(int64(os.Getpid()))
|
|
||||||
}
|
|
||||||
|
|
||||||
var genIdNumBytes = envconst.Int("ZREPL_TRACE_ID_NUM_BYTES", 3)
|
var genIdNumBytes = envconst.Int("ZREPL_TRACE_ID_NUM_BYTES", 3)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -30,7 +21,7 @@ func genID() string {
|
|||||||
enc := base64.NewEncoder(base64.RawStdEncoding, &out)
|
enc := base64.NewEncoder(base64.RawStdEncoding, &out)
|
||||||
buf := make([]byte, genIdNumBytes)
|
buf := make([]byte, genIdNumBytes)
|
||||||
for i := 0; i < len(buf); {
|
for i := 0; i < len(buf); {
|
||||||
n, err := genIdPRNG.Read(buf[i:])
|
n, err := rand.Read(buf[i:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user