Skip to content

Commit

Permalink
Always seed math/rand on consul startup
Browse files Browse the repository at this point in the history
Required for jitter calcs.  This could be done in consul/agent, but this makes it clear it is done only once process-wide.
  • Loading branch information
sean- committed Jan 30, 2016
1 parent 7af6a94 commit 4382c1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rand.go
Original file line number Diff line number Diff line change
@@ -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()) })
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"io/ioutil"
"log"
"os"

"github.com/hashicorp/consul/lib"
)

func init() {
lib.SeedMathRand()
}

func main() {
os.Exit(realMain())
}
Expand Down

0 comments on commit 4382c1f

Please sign in to comment.