Skip to content

Commit

Permalink
seed timeout randomness from crypto/rand to ensure different nodes us…
Browse files Browse the repository at this point in the history
…e different sequences of random timeouts. Fixes issue #50
  • Loading branch information
Simon Fell committed Jul 22, 2015
1 parent 81ff7e0 commit cda1ee8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/big"
"math/rand"
"time"

"github.com/hashicorp/go-msgpack/codec"
)

var rnd = rand.New(rand.NewSource(newSeed()))

func newSeed() int64 {
r, err := crand.Int(crand.Reader, big.NewInt(2^63))
if err != nil {
panic(fmt.Errorf("failed to read random bytes: %v", err))
}
return r.Int64()
}

// randomTimeout returns a value that is between the minVal and 2x minVal.
func randomTimeout(minVal time.Duration) <-chan time.Time {
if minVal == 0 {
return nil
}
extra := (time.Duration(rand.Int63()) % minVal)
extra := (time.Duration(rnd.Int63()) % minVal)
return time.After(minVal + extra)
}

Expand Down

0 comments on commit cda1ee8

Please sign in to comment.