forked from LBRYCommunity/lbry-sdk
fix names and types
This commit is contained in:
parent
011b7f090f
commit
f3a163b382
3 changed files with 13 additions and 11 deletions
|
@ -26,7 +26,7 @@ class TestSessionBloat(IntegrationTestCase):
|
|||
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 1)
|
||||
self.assertFalse(session.is_closing())
|
||||
await asyncio.sleep(1.1)
|
||||
with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)):
|
||||
with self.assertRaises(asyncio.TimeoutError):
|
||||
await session.send_request('server.banner', ())
|
||||
self.assertTrue(session.is_closing())
|
||||
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 0)
|
||||
|
|
|
@ -29,16 +29,16 @@ class ReconnectTests(IntegrationTestCase):
|
|||
await asyncio.wait_for(self.on_transaction_id(sendtxid), 1.0) # mempool
|
||||
await self.blockchain.generate(1)
|
||||
await self.on_transaction_id(sendtxid) # confirmed
|
||||
self.assertLess(self.ledger.network.client.latency, 1) # latency properly set lower, we are fine
|
||||
self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine
|
||||
|
||||
await self.assertBalance(self.account, '1.1337')
|
||||
# is it real? are we rich!? let me see this tx...
|
||||
d = self.ledger.network.get_transaction(sendtxid)
|
||||
# what's that smoke on my ethernet cable? oh no!
|
||||
self.ledger.network.client.connection_lost(Exception())
|
||||
with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)):
|
||||
with self.assertRaises(asyncio.TimeoutError):
|
||||
await d
|
||||
self.assertGreater(self.ledger.network.client.latency, 1000) # latency skyrockets as it failed
|
||||
self.assertIsNone(self.ledger.network.client.response_time) # response time unknown as it failed
|
||||
# rich but offline? no way, no water, let's retry
|
||||
with self.assertRaisesRegex(ConnectionError, 'connection is not available'):
|
||||
await self.ledger.network.get_transaction(sendtxid)
|
||||
|
|
|
@ -23,13 +23,13 @@ class ClientSession(BaseClientSession):
|
|||
self.bw_limit = self.framer.max_size = self.max_errors = 1 << 32
|
||||
self.timeout = timeout
|
||||
self.max_seconds_idle = timeout * 2
|
||||
self.latency = 1 << 32
|
||||
self.response_time: Optional[float] = None
|
||||
self._on_connect_cb = on_connect_callback or (lambda: None)
|
||||
self.trigger_urgent_reconnect = asyncio.Event()
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
return not self.is_closing() and self._can_send.is_set() and self.latency < 1 << 32
|
||||
return not self.is_closing() and self._can_send.is_set() and self.response_time is not None
|
||||
|
||||
async def send_request(self, method, args=()):
|
||||
try:
|
||||
|
@ -37,13 +37,13 @@ class ClientSession(BaseClientSession):
|
|||
result = await asyncio.wait_for(
|
||||
super().send_request(method, args), timeout=self.timeout
|
||||
)
|
||||
self.latency = perf_counter() - start
|
||||
self.response_time = perf_counter() - start
|
||||
return result
|
||||
except RPCError as e:
|
||||
log.warning("Wallet server returned an error. Code: %s Message: %s", *e.args)
|
||||
raise e
|
||||
except TimeoutError:
|
||||
self.latency = 1 << 32
|
||||
self.response_time = None
|
||||
raise
|
||||
|
||||
async def ensure_session(self):
|
||||
|
@ -56,7 +56,7 @@ class ClientSession(BaseClientSession):
|
|||
await self.create_connection(self.timeout)
|
||||
await self.ensure_server_version()
|
||||
self._on_connect_cb()
|
||||
if (time() - self.last_send) > self.max_seconds_idle or self.latency == 1 << 32:
|
||||
if (time() - self.last_send) > self.max_seconds_idle or self.response_time is None:
|
||||
await self.send_request('server.banner')
|
||||
retry_delay = default_delay
|
||||
except (asyncio.TimeoutError, OSError):
|
||||
|
@ -84,7 +84,7 @@ class ClientSession(BaseClientSession):
|
|||
def connection_lost(self, exc):
|
||||
log.debug("Connection lost: %s:%d", *self.server)
|
||||
super().connection_lost(exc)
|
||||
self.latency = 1 << 32
|
||||
self.response_time = None
|
||||
self._on_disconnect_controller.add(True)
|
||||
|
||||
|
||||
|
@ -202,7 +202,9 @@ class SessionPool:
|
|||
def fastest_session(self):
|
||||
if not self.available_sessions:
|
||||
return None
|
||||
return min([(session.latency, session) for session in self.available_sessions], key=itemgetter(0))[1]
|
||||
return min(
|
||||
[(session.response_time, session) for session in self.available_sessions], key=itemgetter(0)
|
||||
)[1]
|
||||
|
||||
def start(self, default_servers):
|
||||
callback = self.new_connection_event.set
|
||||
|
|
Loading…
Reference in a new issue