Simplify logic in WalletRequestProcessor.

This commit is contained in:
Josh Rickmar 2014-03-27 22:18:23 -05:00
parent 53e4070a5a
commit aa0980bfa7

View file

@ -180,48 +180,46 @@ var (
) )
// WalletRequestProcessor processes client requests and btcd notifications. // WalletRequestProcessor processes client requests and btcd notifications.
// Notifications are preferred over client requests.
func WalletRequestProcessor() { func WalletRequestProcessor() {
for { for {
select { select {
case r := <-requestQueue: case r := <-requestQueue:
var result interface{} method := r.request.Method()
var jsonErr *btcjson.Error f, ok := rpcHandlers[method]
if f, ok := rpcHandlers[r.request.Method()]; ok { if !ok && r.ws {
AcctMgr.Grab() f, ok = wsHandlers[method]
result, jsonErr = f(r.request)
AcctMgr.Release()
} else if f, ok := wsHandlers[r.request.Method()]; r.ws && ok {
AcctMgr.Grab()
result, jsonErr = f(r.request)
AcctMgr.Release()
} else {
result, jsonErr = Unimplemented(r.request)
} }
resp := &ClientResponse{ if !ok {
f = Unimplemented
}
AcctMgr.Grab()
result, jsonErr := f(r.request)
AcctMgr.Release()
r.response <- &ClientResponse{
result: result, result: result,
err: jsonErr, err: jsonErr,
} }
r.response <- resp
case n := <-handleNtfn: case n := <-handleNtfn:
if f, ok := notificationHandlers[n.Method()]; ok { f, ok := notificationHandlers[n.Method()]
if !ok {
// Ignore unhandled notifications.
continue
}
AcctMgr.Grab() AcctMgr.Grab()
err := f(n) err := f(n)
AcctMgr.Release() AcctMgr.Release()
switch err { switch err {
case nil:
// ignore
case tx.ErrInconsistantStore: case tx.ErrInconsistantStore:
log.Warn("Detected inconsistant TxStore. Reconnecting...") // Assume this is a broken btcd reordered
// Likely due to a mis-ordered btcd notification. // notifications. Restart the connection
// To recover, close server connection and reopen // to reload accounts files from their last
// all accounts from their last good state saved // known good state.
// to disk. This will trigger the handshake on log.Warn("Reconnecting to recover from "+
// next connect, and a rescan of one or two blocks "out-of-order btcd notification")
// to catch up rather than throwing away all tx
// history and rescanning everything.
s := CurrentServerConn() s := CurrentServerConn()
if btcd, ok := s.(*BtcdRPCConn); ok { if btcd, ok := s.(*BtcdRPCConn); ok {
AcctMgr.Grab() AcctMgr.Grab()
@ -230,12 +228,12 @@ func WalletRequestProcessor() {
AcctMgr.Release() AcctMgr.Release()
} }
default: // other non-nil case nil: // ignore
default:
log.Warn(err) log.Warn(err)
} }
} }
} }
}
} }
// Unimplemented handles an unimplemented RPC request with the // Unimplemented handles an unimplemented RPC request with the