Simplify to single heartbeat timer

This commit is contained in:
Adam Harrison committed 2015-02-24 18:31:18 +00:00
1 parent 0a0ed7a871
commit 7618ae033c
1 file changed
+6 -26
+6 -26
View File
@@ -44,7 +44,6 @@ type LocalConnection struct {
stackFrag bool stackFrag bool
effectivePMTU int effectivePMTU int
SessionKey *[32]byte SessionKey *[32]byte
establishedTimeout *time.Timer
heartbeatTimeout *time.Timer heartbeatTimeout *time.Timer
heartbeatFrame *ForwardedFrame heartbeatFrame *ForwardedFrame
heartbeat *time.Ticker heartbeat *time.Ticker
@@ -262,7 +261,7 @@ func (conn *LocalConnection) run(queryChan <-chan *ConnectionInteraction, accept
} }
} }
conn.establishedTimeout = time.NewTimer(EstablishedTimeout) conn.heartbeatTimeout = time.NewTimer(HeartbeatTimeout)
if err := conn.queryLoop(queryChan); err != nil { if err := conn.queryLoop(queryChan); err != nil {
conn.log("connection shutting down due to error:", err) conn.log("connection shutting down due to error:", err)
@@ -297,16 +296,11 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction)
case CReceivedHeartbeat: case CReceivedHeartbeat:
err = conn.handleReceivedHeartbeat(query.payload.(*net.UDPAddr)) err = conn.handleReceivedHeartbeat(query.payload.(*net.UDPAddr))
case CSetEstablished: case CSetEstablished:
conn.establishedTimeout.Stop()
err = conn.handleSetEstablished() err = conn.handleSetEstablished()
case CSendProtocolMsg: case CSendProtocolMsg:
err = conn.handleSendProtocolMsg(query.payload.(ProtocolMsg)) err = conn.handleSendProtocolMsg(query.payload.(ProtocolMsg))
} }
case <-timerChan(conn.establishedTimeout): case <-conn.heartbeatTimeout.C:
if !conn.established {
err = fmt.Errorf("failed to establish UDP connectivity")
}
case <-timerChan(conn.heartbeatTimeout):
err = fmt.Errorf("timed out waiting for UDP heartbeat") err = fmt.Errorf("timed out waiting for UDP heartbeat")
case <-tickerChan(conn.heartbeat): case <-tickerChan(conn.heartbeat):
conn.Forward(true, conn.heartbeatFrame, nil) conn.Forward(true, conn.heartbeatFrame, nil)
@@ -332,9 +326,7 @@ func (conn *LocalConnection) handleReceivedHeartbeat(remoteUDPAddr *net.UDPAddr)
conn.remoteUDPAddr = remoteUDPAddr conn.remoteUDPAddr = remoteUDPAddr
conn.receivedHeartbeat = true conn.receivedHeartbeat = true
conn.Unlock() conn.Unlock()
if conn.established {
conn.heartbeatTimeout.Reset(HeartbeatTimeout) conn.heartbeatTimeout.Reset(HeartbeatTimeout)
}
if !old { if !old {
if err := conn.handleSendSimpleProtocolMsg(ProtocolConnectionEstablished); err != nil { if err := conn.handleSendSimpleProtocolMsg(ProtocolConnectionEstablished); err != nil {
return err return err
@@ -368,7 +360,6 @@ func (conn *LocalConnection) handleSetEstablished() error {
dstPeer: conn.remote, dstPeer: conn.remote,
frame: PMTUDiscovery}, frame: PMTUDiscovery},
nil) nil)
conn.heartbeatTimeout = time.NewTimer(HeartbeatTimeout)
conn.heartbeat = time.NewTicker(SlowHeartbeat) conn.heartbeat = time.NewTicker(SlowHeartbeat)
conn.fragTest = time.NewTicker(FragTestInterval) conn.fragTest = time.NewTicker(FragTestInterval)
// avoid initial waits for timers to fire // avoid initial waits for timers to fire
@@ -398,8 +389,10 @@ func (conn *LocalConnection) handleShutdown() {
conn.Router.Ourself.DeleteConnection(conn) conn.Router.Ourself.DeleteConnection(conn)
} }
stopTimer(conn.establishedTimeout) if conn.heartbeatTimeout != nil {
stopTimer(conn.heartbeatTimeout) conn.heartbeatTimeout.Stop()
}
stopTicker(conn.heartbeat) stopTicker(conn.heartbeat)
stopTicker(conn.fragTest) stopTicker(conn.fragTest)
@@ -503,16 +496,3 @@ func stopTicker(ticker *time.Ticker) {
ticker.Stop() ticker.Stop()
} }
} }
func timerChan(timer *time.Timer) <-chan time.Time {
if timer != nil {
return timer.C
}
return nil
}
func stopTimer(timer *time.Timer) {
if timer != nil {
timer.Stop()
}
}