Fixing shutdown flow

This commit is contained in:
Sarah Jamie Lewis 2019-07-24 11:49:01 -07:00
parent 29852508d9
commit 101cce532f
1 changed files with 11 additions and 4 deletions

View File

@ -36,6 +36,8 @@ type engine struct {
// Required for listen(), inaccessible from identity // Required for listen(), inaccessible from identity
privateKey ed25519.PrivateKey privateKey ed25519.PrivateKey
shuttingDown bool
} }
// Engine (ProtocolEngine) encapsulates the logic necessary to make and receive Cwtch connections. // Engine (ProtocolEngine) encapsulates the logic necessary to make and receive Cwtch connections.
@ -133,10 +135,10 @@ func (e *engine) eventHandler() {
func (e *engine) createPeerTemplate() *PeerApp { func (e *engine) createPeerTemplate() *PeerApp {
peerAppTemplate := new(PeerApp) peerAppTemplate := new(PeerApp)
peerAppTemplate.MessageHandler = e.handlePeerMessage peerAppTemplate.MessageHandler = e.handlePeerMessage
peerAppTemplate.OnAcknowledgement = e.peerAck peerAppTemplate.OnAcknowledgement = e.ignoreOnShutdown(e.peerAck)
peerAppTemplate.OnAuth = e.peerAuthed peerAppTemplate.OnAuth = e.ignoreOnShutdown(e.peerAuthed)
peerAppTemplate.OnConnecting = e.peerConnecting peerAppTemplate.OnConnecting = e.ignoreOnShutdown(e.peerConnecting)
peerAppTemplate.OnClose = e.peerDisconnected peerAppTemplate.OnClose = e.ignoreOnShutdown(e.peerDisconnected)
return peerAppTemplate return peerAppTemplate
} }
@ -149,6 +151,7 @@ func (e *engine) listenFn() {
// Shutdown tears down the eventHandler goroutine // Shutdown tears down the eventHandler goroutine
func (e *engine) Shutdown() { func (e *engine) Shutdown() {
e.shuttingDown = true
e.service.Shutdown() e.service.Shutdown()
e.connectionsManager.Shutdown() e.connectionsManager.Shutdown()
e.queue.Shutdown() e.queue.Shutdown()
@ -159,6 +162,10 @@ func (e *engine) peerWithOnion(onion string) {
e.service.Connect(onion, e.createPeerTemplate()) e.service.Connect(onion, e.createPeerTemplate())
} }
func (e * engine) ignoreOnShutdown(f func(string)) func(string){
return func (x string) {if !e.shuttingDown{f(x)} }
}
func (e *engine) peerAuthed(onion string) { func (e *engine) peerAuthed(onion string) {
e.eventManager.Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{ e.eventManager.Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{
event.RemotePeer: string(onion), event.RemotePeer: string(onion),