Simplify _is_rpc_available

This commit is contained in:
Neil Booth 2018-08-13 10:23:30 +09:00
parent 4e40e26ac4
commit 08347fe275

View File

@ -33,7 +33,6 @@ class Daemon(object):
'''Handles connections to a daemon at the given URL.''' '''Handles connections to a daemon at the given URL.'''
WARMING_UP = -28 WARMING_UP = -28
RPC_MISC_ERROR = -1
id_counter = itertools.count() id_counter = itertools.count()
class DaemonWarmingUpError(Exception): class DaemonWarmingUpError(Exception):
@ -47,7 +46,7 @@ class Daemon(object):
# Limit concurrent RPC calls to this number. # Limit concurrent RPC calls to this number.
# See DEFAULT_HTTP_WORKQUEUE in bitcoind, which is typically 16 # See DEFAULT_HTTP_WORKQUEUE in bitcoind, which is typically 16
self.workqueue_semaphore = asyncio.Semaphore(value=max_workqueue) self.workqueue_semaphore = asyncio.Semaphore(value=max_workqueue)
self._available_rpcs = {} # caches results for _is_rpc_available() self.available_rpcs = {}
def set_urls(self, urls): def set_urls(self, urls):
'''Set the URLS to the given list, and switch to the first one.''' '''Set the URLS to the given list, and switch to the first one.'''
@ -185,26 +184,16 @@ class Daemon(object):
Results are cached and the daemon will generally not be queried with Results are cached and the daemon will generally not be queried with
the same method more than once.''' the same method more than once.'''
available = self._available_rpcs.get(method, None) available = self.available_rpcs.get(method)
if available is None: if available is None:
available = True
try: try:
await self._send_single(method) await self._send_single(method)
available = True
except DaemonError as e: except DaemonError as e:
err = e.args[0] err = e.args[0]
error_code = err.get("code") error_code = err.get("code")
if error_code == JSONRPC.METHOD_NOT_FOUND: available = error_code != JSONRPC.METHOD_NOT_FOUND
available = False self.available_rpcs[method] = available
elif error_code == self.RPC_MISC_ERROR:
# method found but exception was thrown in command handling
# probably because we did not provide arguments
available = True
else:
self.logger.warning(f'error (code {error_code}: '
f'{err.get("message")}) testing '
f'RPC availability of method {method}')
available = False
self._available_rpcs[method] = available
return available return available
async def block_hex_hashes(self, first, count): async def block_hex_hashes(self, first, count):