git rid of heartbeater goroutine

...by moving the logic into the connection actor goroutine.

The heartbeater goroutine was doing a lot more than just heartbeating;
namely it also had tickers for triggering a ProtocolFetchAll and
initiating a fragmentation test. Furhermore, ensureHeartbeats also
spawned the forwarders.

So this was quite a mix of functionality, and, furthermore, most of it
related quite closely to the connection actor. So moving the
functionality into that simplifies the code.

In the process we also fix #294.
This commit is contained in:
Matthias Radestock committed 2014-12-23 22:01:03 +00:00
1 parent ad67dc5d07
commit e850127ed4
3 files changed
+62 -73

No files matched your search

+4 -6
View File
@@ -66,10 +66,9 @@ Router:
simply responds to traffic received from the remote peer via TCP simply responds to traffic received from the remote peer via TCP
5. We register this connection with the local peer 5. We register this connection with the local peer
6a. If we initiated this connection then we now start sending fast 6a. If we initiated this connection then we now start sending fast
heartbeats (a new thread is spawned to do the heartbeating) to the heartbeats to the remote peer so that the remote peer can determine what
remote peer so that the remote peer can determine what
address/port it should use to send UDP back to us. To do this, we address/port it should use to send UDP back to us. To do this, we
also spawn off two further threads which are the forwarder spawn off two further threads which are the forwarder
loops. These receive frames which are to be sent to the remote loops. These receive frames which are to be sent to the remote
peer. One of them sends frames without the DF flag set. To do this peer. One of them sends frames without the DF flag set. To do this
it just sends the packets out of the UDP Listener socket. The it just sends the packets out of the UDP Listener socket. The
@@ -84,9 +83,8 @@ Router:
local peer. It will tell the local connection (communicating to local peer. It will tell the local connection (communicating to
the actor thread) about the UDP address of the remote peer. The the actor thread) about the UDP address of the remote peer. The
local connection will then start its forwarder threads as local connection will then start its forwarder threads as
described in 6a, and start sending slow heartbeats (heartbeating described in 6a, and start sending slow heartbeats. The local connection
is done by another process). The local connection is marked as is marked as established which means it is included in network updates
established which means it is included in network updates
broadcast to our peers. We also send to the remote peer via TCP a broadcast to our peers. We also send to the remote peer via TCP a
ConnectionEstablished message. The remote peer receives this (on ConnectionEstablished message. The remote peer receives this (on
the TCP receiver process), tells the connection actor process, the TCP receiver process), tells the connection actor process,
+55 -66
View File
@@ -105,10 +105,9 @@ func (conn *LocalConnection) setEffectivePMTU(pmtu int) {
} }
} }
// Called by the connection's actor process, by the connection's // Called by the connection's actor process, and by the connection's
// heartbeat process and by the connection's TCP received // TCP received process. StackFrag is read in conn.Forward (called by
// process. StackFrag is read in conn.Forward (called by router udp // router udp listener and sniffer processes)
// listener and sniffer processes)
func (conn *LocalConnection) setStackFrag(frag bool) { func (conn *LocalConnection) setStackFrag(frag bool) {
conn.Lock() conn.Lock()
defer conn.Unlock() defer conn.Unlock()
@@ -168,27 +167,50 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
} }
conn.local.AddConnection(conn) conn.local.AddConnection(conn)
if conn.remoteUDPAddr != nil { if conn.remoteUDPAddr != nil {
err = conn.ensureHeartbeat(true) if err = conn.ensureForwarders(); err == nil {
conn.heartbeat = time.NewTicker(FastHeartbeat)
conn.forwardHeartbeatFrame() // avoid initial wait
}
} }
var heartbeat, fetchAll, fragTest <-chan time.Time
terminate := false terminate := false
for !terminate { for !terminate {
if err != nil { if err != nil {
conn.log("error:", err) conn.log("error:", err)
break break
} }
query, ok := <-queryChan if conn.heartbeat != nil {
if !ok { heartbeat = conn.heartbeat.C
break
} }
switch query.code { if conn.fetchAll != nil {
case CShutdown: fetchAll = conn.fetchAll.C
terminate = true }
case CSetEstablished: if conn.fragTest != nil {
err = conn.handleSetEstablished() fragTest = conn.fragTest.C
case CSetRemoteUDPAddr: }
err = conn.handleSetRemoteUDPAddr(query.payload.(*net.UDPAddr)) select {
case CSendTCP: case query, ok := <-queryChan:
err = conn.handleSendTCP(query.payload.([]byte)) if !ok {
break
}
switch query.code {
case CShutdown:
terminate = true
case CSetEstablished:
err = conn.handleSetEstablished()
case CSetRemoteUDPAddr:
err = conn.handleSetRemoteUDPAddr(query.payload.(*net.UDPAddr))
case CSendTCP:
err = conn.handleSendTCP(query.payload.([]byte))
}
case <-heartbeat:
conn.forwardHeartbeatFrame()
case <-fetchAll:
err = conn.handleSendTCP(ProtocolFetchAllByte)
case <-fragTest:
conn.setStackFrag(false)
err = conn.handleSendTCP(ProtocolStartFragmentationTestByte)
} }
} }
conn.handleShutdown() conn.handleShutdown()
@@ -220,9 +242,14 @@ func (conn *LocalConnection) handleSetEstablished() error {
conn.Unlock() conn.Unlock()
if !old { if !old {
conn.local.ConnectionEstablished(conn) conn.local.ConnectionEstablished(conn)
if err := conn.ensureHeartbeat(false); err != nil { if err := conn.ensureForwarders(); err != nil {
return err return err
} }
stopTicker(conn.heartbeat)
conn.heartbeat = time.NewTicker(SlowHeartbeat)
conn.fetchAll = time.NewTicker(FetchAllInterval)
conn.fragTest = time.NewTicker(FragTestInterval)
conn.forwardHeartbeatFrame() // avoid initial wait
// Send a large frame down the DF channel in order to prompt // Send a large frame down the DF channel in order to prompt
// PMTU discovery to start. // PMTU discovery to start.
conn.Forward(true, &ForwardedFrame{ conn.Forward(true, &ForwardedFrame{
@@ -266,11 +293,9 @@ func (conn *LocalConnection) handleShutdown() {
conn.local.DeleteConnection(conn) conn.local.DeleteConnection(conn)
} }
if conn.heartbeatStop != nil { stopTicker(conn.heartbeat)
// heartbeatStop is 0 length, so this send will synchronise stopTicker(conn.fetchAll)
// with the receive. stopTicker(conn.fragTest)
conn.heartbeatStop <- nil
}
// blank out the forwardChan so that the router processes don't // blank out the forwardChan so that the router processes don't
// try to send any more // try to send any more
@@ -502,56 +527,20 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder, usingPassword bool
} }
} }
// Heartbeats
// Heartbeating serves two purposes: a) keeping NAT paths alive, and // Heartbeating serves two purposes: a) keeping NAT paths alive, and
// b) updating a remote peer's knowledge of our address, in the event // b) updating a remote peer's knowledge of our address, in the event
// it changes (e.g. because NAT paths expired). // it changes (e.g. because NAT paths expired).
// Called only by connection actor process. // Called only by connection actor process.
func (conn *LocalConnection) ensureHeartbeat(fast bool) error { func (conn *LocalConnection) forwardHeartbeatFrame() {
if err := conn.ensureForwarders(); err != nil {
return err
}
var heartbeat, fetchAll, fragTest <-chan time.Time
// explicitly 0 length chan - make send block until receive occurs
stop := make(chan interface{}, 0)
if fast {
// fast, nofetchall, no fragtest
// Lang Spec: "A nil channel is never ready for communication."
heartbeat = time.Tick(FastHeartbeat)
} else {
heartbeat = time.Tick(SlowHeartbeat)
fetchAll = time.Tick(FetchAllInterval)
fragTest = time.Tick(FragTestInterval)
}
// Don't need locks here as this is only read here and in
// handleShutdown, both of which are called by the connection
// actor process only.
if conn.heartbeatStop != nil {
conn.heartbeatStop <- nil
}
conn.heartbeatStop = stop
go conn.forwardHeartbeats(heartbeat, fetchAll, fragTest, stop)
return nil
}
func (conn *LocalConnection) forwardHeartbeats(heartbeat, fetchAll, fragTest <-chan time.Time, stop <-chan interface{}) {
heartbeatFrame := &ForwardedFrame{ heartbeatFrame := &ForwardedFrame{
srcPeer: conn.local, srcPeer: conn.local,
dstPeer: conn.remote, dstPeer: conn.remote,
frame: []byte{}} frame: []byte{}}
conn.Forward(true, heartbeatFrame, nil) // avoid initial wait conn.Forward(true, heartbeatFrame, nil)
for { }
select {
case <-stop: func stopTicker(ticker *time.Ticker) {
return if ticker != nil {
case <-heartbeat: ticker.Stop()
conn.Forward(true, heartbeatFrame, nil)
case <-fetchAll:
conn.SendTCP(ProtocolFetchAllByte)
case <-fragTest:
conn.setStackFrag(false)
conn.SendTCP(ProtocolStartFragmentationTestByte)
}
} }
} }
+3 -1
View File
@@ -67,7 +67,9 @@ type LocalConnection struct {
stackFrag bool stackFrag bool
effectivePMTU int effectivePMTU int
SessionKey *[32]byte SessionKey *[32]byte
heartbeatStop chan<- interface{} heartbeat *time.Ticker
fetchAll *time.Ticker
fragTest *time.Ticker
forwardChan chan<- *ForwardedFrame forwardChan chan<- *ForwardedFrame
forwardChanDF chan<- *ForwardedFrame forwardChanDF chan<- *ForwardedFrame
stopForward chan<- interface{} stopForward chan<- interface{}