interface.session: add default timeout to send_request
This commit is contained in:
parent
fffec71fb3
commit
b279d351d8
@ -53,8 +53,9 @@ class NotificationSession(ClientSession):
|
|||||||
self.scripthash = scripthash
|
self.scripthash = scripthash
|
||||||
self.header = header
|
self.header = header
|
||||||
|
|
||||||
@aiosafe
|
|
||||||
async def handle_request(self, request):
|
async def handle_request(self, request):
|
||||||
|
# note: if server sends malformed request and we raise, the superclass
|
||||||
|
# will catch the exception, count errors, and at some point disconnect
|
||||||
if isinstance(request, Notification):
|
if isinstance(request, Notification):
|
||||||
if request.method == 'blockchain.scripthash.subscribe' and self.scripthash is not None:
|
if request.method == 'blockchain.scripthash.subscribe' and self.scripthash is not None:
|
||||||
scripthash, status = request.args
|
scripthash, status = request.args
|
||||||
@ -65,6 +66,13 @@ class NotificationSession(ClientSession):
|
|||||||
else:
|
else:
|
||||||
assert False, request.method
|
assert False, request.method
|
||||||
|
|
||||||
|
async def send_request(self, *args, timeout=-1, **kwargs):
|
||||||
|
if timeout == -1:
|
||||||
|
timeout = 20 if not self.proxy else 30
|
||||||
|
return await asyncio.wait_for(
|
||||||
|
super().send_request(*args, **kwargs),
|
||||||
|
timeout)
|
||||||
|
|
||||||
|
|
||||||
# FIXME this is often raised inside a TaskGroup, but then it's not silent :(
|
# FIXME this is often raised inside a TaskGroup, but then it's not silent :(
|
||||||
class GracefulDisconnect(AIOSafeSilentException): pass
|
class GracefulDisconnect(AIOSafeSilentException): pass
|
||||||
@ -262,7 +270,7 @@ class Interface(PrintError):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_block_header(self, height, assert_mode):
|
async def get_block_header(self, height, assert_mode):
|
||||||
res = await asyncio.wait_for(self.session.send_request('blockchain.block.header', [height]), 5)
|
res = await self.session.send_request('blockchain.block.header', [height], timeout=5)
|
||||||
return blockchain.deserialize_header(bytes.fromhex(res), height)
|
return blockchain.deserialize_header(bytes.fromhex(res), height)
|
||||||
|
|
||||||
async def request_chunk(self, idx, tip):
|
async def request_chunk(self, idx, tip):
|
||||||
@ -304,7 +312,7 @@ class Interface(PrintError):
|
|||||||
self.tip = new_header['block_height']
|
self.tip = new_header['block_height']
|
||||||
await copy_header_queue.put(new_header)
|
await copy_header_queue.put(new_header)
|
||||||
except concurrent.futures.TimeoutError:
|
except concurrent.futures.TimeoutError:
|
||||||
await asyncio.wait_for(self.session.send_request('server.ping'), 10)
|
await self.session.send_request('server.ping', timeout=10)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.fut.cancel()
|
self.fut.cancel()
|
||||||
|
|||||||
@ -695,7 +695,7 @@ class Network(PrintError):
|
|||||||
size = max(size, 0)
|
size = max(size, 0)
|
||||||
try:
|
try:
|
||||||
self.requested_chunks.add(index)
|
self.requested_chunks.add(index)
|
||||||
res = await asyncio.wait_for(session.send_request('blockchain.block.headers', [index * 2016, size]), 20)
|
res = await session.send_request('blockchain.block.headers', [index * 2016, size])
|
||||||
finally:
|
finally:
|
||||||
try: self.requested_chunks.remove(index)
|
try: self.requested_chunks.remove(index)
|
||||||
except KeyError: pass
|
except KeyError: pass
|
||||||
@ -816,4 +816,4 @@ class Network(PrintError):
|
|||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
async def attempt_fee_estimate_update(self):
|
async def attempt_fee_estimate_update(self):
|
||||||
await asyncio.wait_for(self.request_fee_estimates(self.interface), 5)
|
await self.request_fee_estimates(self.interface)
|
||||||
|
|||||||
@ -120,7 +120,7 @@ class Synchronizer(PrintError):
|
|||||||
await group.spawn(self.get_transaction, tx_hash)
|
await group.spawn(self.get_transaction, tx_hash)
|
||||||
|
|
||||||
async def get_transaction(self, tx_hash):
|
async def get_transaction(self, tx_hash):
|
||||||
result = await asyncio.wait_for(self.session.send_request('blockchain.transaction.get', [tx_hash]), 20)
|
result = await self.session.send_request('blockchain.transaction.get', [tx_hash])
|
||||||
tx = Transaction(result)
|
tx = Transaction(result)
|
||||||
try:
|
try:
|
||||||
tx.deserialize()
|
tx.deserialize()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user