diff --git a/util.go b/util.go new file mode 100644 index 0000000..47e0c7d --- /dev/null +++ b/util.go @@ -0,0 +1,10 @@ +package elect + +import ( + "math/rand" + "time" +) + +func RandDurationN(n time.Duration) time.Duration { + return time.Duration(rand.Int63n(int64(n))) //nolint:gosec +} diff --git a/voter.go b/voter.go index 1e6a6c2..d38bcff 100644 --- a/voter.go +++ b/voter.go @@ -4,7 +4,6 @@ import ( "crypto/hmac" "encoding/json" "log" - "math/rand" "time" "github.com/dchest/uniuri" @@ -79,13 +78,13 @@ func (v *Voter) loop() { } func (v *Voter) poll() bool { - t := time.NewTimer(randDurationN(v.period)) + t := time.NewTimer(RandDurationN(v.period)) defer t.Stop() t2 := &time.Timer{} if v.vote.NumPollsSinceChange <= 10 { - t2 = time.NewTimer(randDurationN(maxFastVotePeriod)) + t2 = time.NewTimer(RandDurationN(maxFastVotePeriod)) defer t2.Stop() } @@ -168,7 +167,3 @@ func (v *Voter) sendVote() { func (v *Voter) log(format string, args ...any) { log.Printf("[voter] "+format, args...) } - -func randDurationN(n time.Duration) time.Duration { - return time.Duration(rand.Int63n(int64(n))) //nolint:gosec -}