Always seed math/rand on consul startup

Required for jitter calcs.  This could be done in consul/agent, but this makes it clear it is done only once process-wide.
This commit is contained in:
Sean Chittenden 2016-01-29 17:00:08 -08:00
parent c470553b6b
commit 893f91b0d5
2 changed files with 24 additions and 0 deletions

18
lib/rand.go Normal file
View File

@ -0,0 +1,18 @@
package lib
import (
"math/rand"
"sync"
"time"
)
var (
once sync.Once
)
// SeedMathRand provides weak, but guaranteed seeding, which is better than
// running with Go's default seed of 1. A call to SeedMathRand() is expected
// to be called via init(), but never a second time.
func SeedMathRand() {
once.Do(func() { rand.Seed(time.Now().UTC().UnixNano()) })
}

View File

@ -6,8 +6,14 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"github.com/hashicorp/consul/lib"
) )
func init() {
lib.SeedMathRand()
}
func main() { func main() {
os.Exit(realMain()) os.Exit(realMain())
} }