fix names and types

This commit is contained in:
Victor Shyba 2019-08-12 18:16:53 -03:00
parent 011b7f090f
commit f3a163b382
3 changed files with 13 additions and 11 deletions

View file

@ -26,7 +26,7 @@ class TestSessionBloat(IntegrationTestCase):
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 1) self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 1)
self.assertFalse(session.is_closing()) self.assertFalse(session.is_closing())
await asyncio.sleep(1.1) await asyncio.sleep(1.1)
with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)): with self.assertRaises(asyncio.TimeoutError):
await session.send_request('server.banner', ()) await session.send_request('server.banner', ())
self.assertTrue(session.is_closing()) self.assertTrue(session.is_closing())
self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 0) self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 0)

View file

@ -29,16 +29,16 @@ class ReconnectTests(IntegrationTestCase):
await asyncio.wait_for(self.on_transaction_id(sendtxid), 1.0) # mempool await asyncio.wait_for(self.on_transaction_id(sendtxid), 1.0) # mempool
await self.blockchain.generate(1) await self.blockchain.generate(1)
await self.on_transaction_id(sendtxid) # confirmed 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') await self.assertBalance(self.account, '1.1337')
# is it real? are we rich!? let me see this tx... # is it real? are we rich!? let me see this tx...
d = self.ledger.network.get_transaction(sendtxid) d = self.ledger.network.get_transaction(sendtxid)
# what's that smoke on my ethernet cable? oh no! # what's that smoke on my ethernet cable? oh no!
self.ledger.network.client.connection_lost(Exception()) self.ledger.network.client.connection_lost(Exception())
with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)): with self.assertRaises(asyncio.TimeoutError):
await d 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 # rich but offline? no way, no water, let's retry
with self.assertRaisesRegex(ConnectionError, 'connection is not available'): with self.assertRaisesRegex(ConnectionError, 'connection is not available'):
await self.ledger.network.get_transaction(sendtxid) await self.ledger.network.get_transaction(sendtxid)

View file

@ -23,13 +23,13 @@ class ClientSession(BaseClientSession):
self.bw_limit = self.framer.max_size = self.max_errors = 1 << 32 self.bw_limit = self.framer.max_size = self.max_errors = 1 << 32
self.timeout = timeout self.timeout = timeout
self.max_seconds_idle = timeout * 2 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._on_connect_cb = on_connect_callback or (lambda: None)
self.trigger_urgent_reconnect = asyncio.Event() self.trigger_urgent_reconnect = asyncio.Event()
@property @property
def available(self): 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=()): async def send_request(self, method, args=()):
try: try:
@ -37,13 +37,13 @@ class ClientSession(BaseClientSession):
result = await asyncio.wait_for( result = await asyncio.wait_for(
super().send_request(method, args), timeout=self.timeout super().send_request(method, args), timeout=self.timeout
) )
self.latency = perf_counter() - start self.response_time = perf_counter() - start
return result return result
except RPCError as e: except RPCError as e:
log.warning("Wallet server returned an error. Code: %s Message: %s", *e.args) log.warning("Wallet server returned an error. Code: %s Message: %s", *e.args)
raise e raise e
except TimeoutError: except TimeoutError:
self.latency = 1 << 32 self.response_time = None
raise raise
async def ensure_session(self): async def ensure_session(self):
@ -56,7 +56,7 @@ class ClientSession(BaseClientSession):
await self.create_connection(self.timeout) await self.create_connection(self.timeout)
await self.ensure_server_version() await self.ensure_server_version()
self._on_connect_cb() 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') await self.send_request('server.banner')
retry_delay = default_delay retry_delay = default_delay
except (asyncio.TimeoutError, OSError): except (asyncio.TimeoutError, OSError):
@ -84,7 +84,7 @@ class ClientSession(BaseClientSession):
def connection_lost(self, exc): def connection_lost(self, exc):
log.debug("Connection lost: %s:%d", *self.server) log.debug("Connection lost: %s:%d", *self.server)
super().connection_lost(exc) super().connection_lost(exc)
self.latency = 1 << 32 self.response_time = None
self._on_disconnect_controller.add(True) self._on_disconnect_controller.add(True)
@ -202,7 +202,9 @@ class SessionPool:
def fastest_session(self): def fastest_session(self):
if not self.available_sessions: if not self.available_sessions:
return None 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): def start(self, default_servers):
callback = self.new_connection_event.set callback = self.new_connection_event.set