Skip to content

Commit

Permalink
Merge pull request #808 from hashicorp/b-seed-rand
Browse files Browse the repository at this point in the history
Seed the servers random number generator
  • Loading branch information
dadgar committed Feb 17, 2016
2 parents 36675f7 + 3c4bd0a commit 71e40a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ func NewServer(config *Config) (*Server, error) {
// Emit metrics
go s.heartbeatStats()

// Seed the global random.
if err := seedRandom(); err != nil {
return nil, err
}

// Done
return s, nil
}
Expand Down
15 changes: 15 additions & 0 deletions nomad/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nomad

import (
"fmt"
"math"
"math/big"
"math/rand"
"net"
"os"
Expand All @@ -10,6 +12,8 @@ import (
"strconv"
"time"

crand "crypto/rand"

"github.com/hashicorp/serf/serf"
)

Expand Down Expand Up @@ -127,3 +131,14 @@ func rateScaledInterval(rate float64, min time.Duration, n int) time.Duration {
}
return interval
}

// seedRandom seeds the global random variable using a cryptographically random
// seed. It returns an error if determing the random seed fails.
func seedRandom() error {
n, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
return err
}
rand.Seed(n.Int64())
return nil
}

0 comments on commit 71e40a0

Please sign in to comment.