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.
This commit is contained in:
Josh Rickmar 2014-03-17 16:00:04 -05:00
parent 5027acc348
commit 25b7b7ea84

View file

@ -158,13 +158,20 @@ func (btcd *BtcdRPCConn) Start() {
if err := btcd.send(rpcrequest); err != nil { if err := btcd.send(rpcrequest); err != nil {
// Connection lost. // Connection lost.
log.Infof("Cannot complete btcd websocket send: %v",
err)
btcd.ws.Close() btcd.ws.Close()
close(done) close(done)
} }
addrequest.ResponseChan <- rpcrequest.response addrequest.ResponseChan <- rpcrequest.response
case recvResponse := <-responses: case recvResponse, ok := <-responses:
if !ok {
responses = nil
close(done)
break
}
rpcrequest, ok := m[recvResponse.id] rpcrequest, ok := m[recvResponse.id]
if !ok { if !ok {
log.Warnf("Received unexpected btcd response") log.Warnf("Received unexpected btcd response")
@ -197,10 +204,10 @@ func (btcd *BtcdRPCConn) Start() {
rpcrequest.response <- response rpcrequest.response <- response
case <-done: case <-done:
response := &ServerResponse{
err: &ErrBtcdDisconnected,
}
for _, request := range m { for _, request := range m {
response := &ServerResponse{
err: &ErrBtcdDisconnected,
}
request.response <- response request.response <- response
} }
return return
@ -218,8 +225,10 @@ func (btcd *BtcdRPCConn) Start() {
for { for {
var m string var m string
if err := websocket.Message.Receive(btcd.ws, &m); err != nil { if err := websocket.Message.Receive(btcd.ws, &m); err != nil {
log.Debugf("Cannot receive btcd message: %v", err) log.Infof("Cannot receive btcd websocket message: %v",
close(done) err)
btcd.ws.Close()
close(responses)
return return
} }