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,59 +180,57 @@ 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()]
AcctMgr.Grab() if !ok {
err := f(n) // Ignore unhandled notifications.
AcctMgr.Release() continue
switch err { }
case nil:
// ignore
case tx.ErrInconsistantStore: AcctMgr.Grab()
log.Warn("Detected inconsistant TxStore. Reconnecting...") err := f(n)
// Likely due to a mis-ordered btcd notification. AcctMgr.Release()
// To recover, close server connection and reopen switch err {
// all accounts from their last good state saved case tx.ErrInconsistantStore:
// to disk. This will trigger the handshake on // Assume this is a broken btcd reordered
// next connect, and a rescan of one or two blocks // notifications. Restart the connection
// to catch up rather than throwing away all tx // to reload accounts files from their last
// history and rescanning everything. // known good state.
s := CurrentServerConn() log.Warn("Reconnecting to recover from "+
if btcd, ok := s.(*BtcdRPCConn); ok { "out-of-order btcd notification")
AcctMgr.Grab() s := CurrentServerConn()
btcd.Close() if btcd, ok := s.(*BtcdRPCConn); ok {
AcctMgr.OpenAccounts() AcctMgr.Grab()
AcctMgr.Release() btcd.Close()
} AcctMgr.OpenAccounts()
AcctMgr.Release()
default: // other non-nil
log.Warn(err)
} }
case nil: // ignore
default:
log.Warn(err)
} }
} }
} }