Merge pull request #1718 from weaveworks/constant-spacetime-connection-shutdown

LGTM. prevent unbounded number of goroutines for connection shutdown
This commit is contained in:
Bryan Boreham committed 2015-11-25 11:22:08 +00:00
commit 44a2c5fdff
1 file changed
+13 -6
+13 -6
View File
@@ -48,6 +48,7 @@ type LocalConnection struct {
Router *Router Router *Router
uid uint64 uid uint64
actionChan chan<- ConnectionAction actionChan chan<- ConnectionAction
errorChan chan<- error
finished <-chan struct{} // closed to signal that actorLoop has finished finished <-chan struct{} // closed to signal that actorLoop has finished
OverlayConn OverlayConnection OverlayConn OverlayConnection
} }
@@ -87,6 +88,7 @@ func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, ro
log.Fatal("Attempt to create local connection from a peer which is not ourself") log.Fatal("Attempt to create local connection from a peer which is not ourself")
} }
actionChan := make(chan ConnectionAction, ChannelSize) actionChan := make(chan ConnectionAction, ChannelSize)
errorChan := make(chan error, 1)
finished := make(chan struct{}) finished := make(chan struct{})
conn := &LocalConnection{ conn := &LocalConnection{
RemoteConnection: *connRemote, // NB, we're taking a copy of connRemote here. RemoteConnection: *connRemote, // NB, we're taking a copy of connRemote here.
@@ -94,8 +96,9 @@ func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, ro
TCPConn: tcpConn, TCPConn: tcpConn,
uid: randUint64(), uid: randUint64(),
actionChan: actionChan, actionChan: actionChan,
errorChan: errorChan,
finished: finished} finished: finished}
go conn.run(actionChan, finished, acceptNewPeer) go conn.run(actionChan, errorChan, finished, acceptNewPeer)
} }
func (conn *LocalConnection) BreakTie(dupConn Connection) ConnectionTieBreak { func (conn *LocalConnection) BreakTie(dupConn Connection) ConnectionTieBreak {
@@ -135,8 +138,10 @@ func (conn *LocalConnection) Shutdown(err error) {
panic("nil error") panic("nil error")
} }
// Run on its own goroutine in case the channel is backed up select {
go func() { conn.sendAction(func() error { return err }) }() case conn.errorChan <- err:
default:
}
} }
// Send an actor request to the actorLoop, but don't block if // Send an actor request to the actorLoop, but don't block if
@@ -151,7 +156,7 @@ func (conn *LocalConnection) sendAction(action ConnectionAction) {
// ACTOR server // ACTOR server
func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished chan<- struct{}, acceptNewPeer bool) { func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, errorChan <-chan error, finished chan<- struct{}, acceptNewPeer bool) {
var err error // important to use this var and not create another one with 'err :=' var err error // important to use this var and not create another one with 'err :='
defer func() { conn.shutdown(err) }() defer func() { conn.shutdown(err) }()
defer close(finished) defer close(finished)
@@ -237,7 +242,7 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch
// running AddConnection in a separate goroutine, at least not // running AddConnection in a separate goroutine, at least not
// without some synchronisation. Which in turn requires the // without some synchronisation. Which in turn requires the
// launching of the receiveTCP goroutine to precede actorLoop. // launching of the receiveTCP goroutine to precede actorLoop.
err = conn.actorLoop(actionChan) err = conn.actorLoop(actionChan, errorChan)
} }
func (conn *LocalConnection) makeFeatures() map[string]string { func (conn *LocalConnection) makeFeatures() map[string]string {
@@ -329,7 +334,7 @@ func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) er
return nil return nil
} }
func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err error) { func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction, errorChan <-chan error) (err error) {
fwdErrorChan := conn.OverlayConn.ErrorChannel() fwdErrorChan := conn.OverlayConn.ErrorChannel()
fwdEstablishedChan := conn.OverlayConn.EstablishedChannel() fwdEstablishedChan := conn.OverlayConn.EstablishedChannel()
@@ -338,6 +343,8 @@ func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err
case action := <-actionChan: case action := <-actionChan:
err = action() err = action()
case err = <-errorChan:
case <-conn.heartbeatTCP.C: case <-conn.heartbeatTCP.C:
err = conn.sendSimpleProtocolMsg(ProtocolHeartbeat) err = conn.sendSimpleProtocolMsg(ProtocolHeartbeat)