From 25b7b7ea84783771b5a3b7aaa2d80753833f479b Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 17 Mar 2014 16:00:04 -0500 Subject: [PATCH] Improve btcd connection management. This change slightly improves the goroutines managing sends and receives for a btcd connection by improving the logging (logging the exact errors that caused the connection to be lost) as well as cleaning up the shutdown handling by closing the websocket connection for any fail and closing the response channel when no more responses can be read. --- rpcclient.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/rpcclient.go b/rpcclient.go index f0bc16e..986fab4 100644 --- a/rpcclient.go +++ b/rpcclient.go @@ -158,13 +158,20 @@ func (btcd *BtcdRPCConn) Start() { if err := btcd.send(rpcrequest); err != nil { // Connection lost. + log.Infof("Cannot complete btcd websocket send: %v", + err) btcd.ws.Close() close(done) } addrequest.ResponseChan <- rpcrequest.response - case recvResponse := <-responses: + case recvResponse, ok := <-responses: + if !ok { + responses = nil + close(done) + break + } rpcrequest, ok := m[recvResponse.id] if !ok { log.Warnf("Received unexpected btcd response") @@ -197,10 +204,10 @@ func (btcd *BtcdRPCConn) Start() { rpcrequest.response <- response case <-done: + response := &ServerResponse{ + err: &ErrBtcdDisconnected, + } for _, request := range m { - response := &ServerResponse{ - err: &ErrBtcdDisconnected, - } request.response <- response } return @@ -218,8 +225,10 @@ func (btcd *BtcdRPCConn) Start() { for { var m string if err := websocket.Message.Receive(btcd.ws, &m); err != nil { - log.Debugf("Cannot receive btcd message: %v", err) - close(done) + log.Infof("Cannot receive btcd websocket message: %v", + err) + btcd.ws.Close() + close(responses) return }