mirror of
https://github.com/kemko/nomad.git
synced 2026-01-05 09:55:44 +03:00
Use crypto random seed
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/rpc"
|
||||
"os"
|
||||
@@ -249,7 +248,9 @@ func NewServer(config *Config) (*Server, error) {
|
||||
go s.heartbeatStats()
|
||||
|
||||
// Seed the global random.
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
if err := seedRandom(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Done
|
||||
return s, nil
|
||||
|
||||
@@ -2,6 +2,8 @@ package nomad
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
@@ -10,6 +12,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
crand "crypto/rand"
|
||||
|
||||
"github.com/hashicorp/serf/serf"
|
||||
)
|
||||
|
||||
@@ -127,3 +131,15 @@ 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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user