close session before removing from peer list

parent 319bfc1b
......@@ -585,15 +585,14 @@ func (g *Gateway) Disconnect(addr modules.NetAddress) error {
if !exists {
return errors.New("not connected to that node")
}
p.sess.Close()
g.mu.Lock()
// Peer is removed from the peer list as wellas the node list, to prevent
// Peer is removed from the peer list as well as the node list, to prevent
// the node from being re-connected while looking for a replacement peer.
delete(g.peers, addr)
delete(g.nodes, addr)
g.mu.Unlock()
if err := p.sess.Close(); err != nil {
return err
}
g.log.Println("INFO: disconnected from peer", addr)
return nil
......
......@@ -48,12 +48,10 @@ func (g *Gateway) managedRPC(addr modules.NetAddress, name string, fn modules.RP
// peer probably disconnected without sending a shutdown signal;
// disconnect from them
g.log.Debugf("Could not initiate RPC with %v; disconnecting", addr)
peer.sess.Close()
g.mu.Lock()
delete(g.peers, addr)
g.mu.Unlock()
if derr := peer.sess.Close(); err != nil {
g.log.Debugf("Could not cleanly disconnect from %v: %v", addr, derr)
}
return err
}
defer conn.Close()
......@@ -157,13 +155,11 @@ func (g *Gateway) threadedListenPeer(p *peer) {
case <-peerCloseChan:
}
// Can't call Disconnect because it could return sync.ErrStopped.
// Close the session and remove p from the peer list.
p.sess.Close()
g.mu.Lock()
delete(g.peers, p.NetAddress)
g.mu.Unlock()
if err := p.sess.Close(); err != nil {
g.log.Debugf("WARN: error disconnecting from peer %q: %v", p.NetAddress, err)
}
}()
for {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment