Removed exponential backoff

This commit is contained in:
Mathias Hall-Andersen 2017-06-30 23:05:03 +02:00
parent ba3e486667
commit a4cc0a30fa

View file

@ -65,17 +65,16 @@ func (peer *Peer) KeepKeyFreshSending() {
* *
* Associated with this routine is the signal "handshakeBegin" * Associated with this routine is the signal "handshakeBegin"
* The routine will read from the "handshakeBegin" channel * The routine will read from the "handshakeBegin" channel
* at most every RekeyTimeout or with exponential backoff * at most every RekeyTimeout seconds
*
* Implements exponential backoff for retries
*/ */
func (peer *Peer) RoutineHandshakeInitiator() { func (peer *Peer) RoutineHandshakeInitiator() {
work := new(QueueOutboundElement)
device := peer.device device := peer.device
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
logger := device.log.Debug logger := device.log.Debug
timeout := time.NewTimer(time.Hour) timeout := time.NewTimer(time.Hour)
var work *QueueOutboundElement
logger.Println("Routine, handshake initator, started for peer", peer.id) logger.Println("Routine, handshake initator, started for peer", peer.id)
func() { func() {
@ -83,6 +82,8 @@ func (peer *Peer) RoutineHandshakeInitiator() {
var attempts uint var attempts uint
var deadline time.Time var deadline time.Time
// wait for signal
select { select {
case <-peer.signal.handshakeBegin: case <-peer.signal.handshakeBegin:
case <-peer.signal.stop: case <-peer.signal.stop:
@ -90,7 +91,7 @@ func (peer *Peer) RoutineHandshakeInitiator() {
} }
HandshakeLoop: HandshakeLoop:
for run := true; run; { for {
// clear completed signal // clear completed signal
select { select {
@ -100,32 +101,28 @@ func (peer *Peer) RoutineHandshakeInitiator() {
default: default:
} }
// queue handshake
err := func() error {
work.mutex.Lock()
defer work.mutex.Unlock()
// create initiation // create initiation
if work != nil {
work.mutex.Lock()
work.packet = nil
work.mutex.Unlock()
}
work = new(QueueOutboundElement)
msg, err := device.CreateMessageInitiation(peer) msg, err := device.CreateMessageInitiation(peer)
if err != nil { if err != nil {
return err device.log.Error.Println("Failed to create initiation message:", err)
break
} }
// marshal // schedule for sending
writer := bytes.NewBuffer(buffer[:0]) writer := bytes.NewBuffer(buffer[:0])
binary.Write(writer, binary.LittleEndian, msg) binary.Write(writer, binary.LittleEndian, msg)
work.packet = writer.Bytes() work.packet = writer.Bytes()
peer.mac.AddMacs(work.packet) peer.mac.AddMacs(work.packet)
peer.InsertOutbound(work) peer.InsertOutbound(work)
return nil
}()
if err != nil {
device.log.Error.Println("Failed to create initiation message:", err)
break
}
if attempts == 0 { if attempts == 0 {
deadline = time.Now().Add(MaxHandshakeAttemptTime) deadline = time.Now().Add(MaxHandshakeAttemptTime)
} }
@ -138,23 +135,13 @@ func (peer *Peer) RoutineHandshakeInitiator() {
default: default:
} }
} }
timeout.Reset((1 << attempts) * RekeyTimeout)
attempts += 1 attempts += 1
timeout.Reset(RekeyTimeout)
device.log.Debug.Println("Handshake initiation attempt", attempts, "queued for peer", peer.id) device.log.Debug.Println("Handshake initiation attempt", attempts, "queued for peer", peer.id)
time.Sleep(RekeyTimeout)
// wait for handshake or timeout // wait for handshake or timeout
select { select {
case <-peer.signal.stop:
return
case <-peer.signal.handshakeCompleted:
break HandshakeLoop
default:
select {
case <-peer.signal.stop: case <-peer.signal.stop:
return return
@ -162,9 +149,7 @@ func (peer *Peer) RoutineHandshakeInitiator() {
break HandshakeLoop break HandshakeLoop
case <-timeout.C: case <-timeout.C:
nextTimeout := (1 << attempts) * RekeyTimeout if deadline.Before(time.Now().Add(RekeyTimeout)) {
if deadline.Before(time.Now().Add(nextTimeout)) {
// we do not have time for another attempt
peer.signal.flushNonceQueue <- struct{}{} peer.signal.flushNonceQueue <- struct{}{}
if !peer.timer.sendKeepalive.Stop() { if !peer.timer.sendKeepalive.Stop() {
<-peer.timer.sendKeepalive.C <-peer.timer.sendKeepalive.C
@ -174,7 +159,6 @@ func (peer *Peer) RoutineHandshakeInitiator() {
} }
} }
} }
}
}() }()
logger.Println("Routine, handshake initator, stopped for peer", peer.id) logger.Println("Routine, handshake initator, stopped for peer", peer.id)