Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed the servers random number generator #808

Merged
merged 3 commits into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}