diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-05-05 04:42:17 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-05-05 04:42:17 +0200 |
commit | 8ba28be732a3ca9819595dc4cea24d45d0d676c3 (patch) | |
tree | c505c97178584acb6e38d79077ed9a6642511795 /timers.go | |
parent | d8d592787df648dfa1043cfd23e842d53f3b3f27 (diff) |
Use Int32n for jitter
Diffstat (limited to 'timers.go')
-rw-r--r-- | timers.go | 95 |
1 files changed, 48 insertions, 47 deletions
@@ -201,6 +201,53 @@ func (peer *Peer) RoutineTimerHandler() { timerKeepalivePersistent.Reset(duration) } + case <-peer.event.handshakeBegin.C: + + if !enableHandshake { + continue + } + + logDebug.Println(peer, ": Event, Handshake Begin") + + err := peer.sendNewHandshake() + + // set timeout + + jitter := time.Millisecond * time.Duration(rand.Int31n(334)) + timerKeepalivePassive.Stop() + timerHandshakeTimeout.Reset(RekeyTimeout + jitter) + + if err != nil { + logInfo.Println(peer, ": Failed to send handshake initiation", err) + } else { + logDebug.Println(peer, ": Send handshake initiation (initial)") + } + + timerHandshakeDeadline.Reset(RekeyAttemptTime) + + // disable further handshakes + + peer.event.handshakeBegin.Clear() + enableHandshake = false + + case <-peer.event.handshakeCompleted.C: + + logInfo.Println(peer, ": Handshake completed") + + atomic.StoreInt64( + &peer.stats.lastHandshakeNano, + time.Now().UnixNano(), + ) + + timerHandshakeTimeout.Stop() + timerHandshakeDeadline.Stop() + peer.timer.sendLastMinuteHandshake.Set(false) + + // allow further handshakes + + peer.event.handshakeBegin.Clear() + enableHandshake = true + /* timers */ // keep-alive @@ -280,7 +327,7 @@ func (peer *Peer) RoutineTimerHandler() { // set timeout - jitter := time.Millisecond * time.Duration(rand.Uint32()%334) + jitter := time.Millisecond * time.Duration(rand.Int31n(334)) timerKeepalivePassive.Stop() timerHandshakeTimeout.Reset(RekeyTimeout + jitter) @@ -310,52 +357,6 @@ func (peer *Peer) RoutineTimerHandler() { peer.event.handshakeBegin.Clear() enableHandshake = true - case <-peer.event.handshakeBegin.C: - - if !enableHandshake { - continue - } - - logDebug.Println(peer, ": Event, Handshake Begin") - - err := peer.sendNewHandshake() - - // set timeout - - jitter := time.Millisecond * time.Duration(rand.Uint32()%334) - timerKeepalivePassive.Stop() - timerHandshakeTimeout.Reset(RekeyTimeout + jitter) - - if err != nil { - logInfo.Println(peer, ": Failed to send handshake initiation", err) - } else { - logDebug.Println(peer, ": Send handshake initiation (initial)") - } - - timerHandshakeDeadline.Reset(RekeyAttemptTime) - - // disable further handshakes - - peer.event.handshakeBegin.Clear() - enableHandshake = false - - case <-peer.event.handshakeCompleted.C: - - logInfo.Println(peer, ": Handshake completed") - - atomic.StoreInt64( - &peer.stats.lastHandshakeNano, - time.Now().UnixNano(), - ) - - timerHandshakeTimeout.Stop() - timerHandshakeDeadline.Stop() - peer.timer.sendLastMinuteHandshake.Set(false) - - // allow further handshakes - - peer.event.handshakeBegin.Clear() - enableHandshake = true } } } |