def test_retry_logic(self): peer = PeerId('testnet') peer.retry_attempts = settings.MAX_PEER_CONNECTION_ATTEMPS self.assertFalse(peer.can_retry(0)) peer.retry_attempts = 0 # should still fail as the RETRIES_EXCEEDED flag is already set self.assertFalse(peer.can_retry(0)) # remove flag and try again from hathor.p2p.peer_id import PeerFlags peer.flags.remove(PeerFlags.RETRIES_EXCEEDED) self.assertTrue(peer.can_retry(0)) peer.retry_timestamp = 100 self.assertFalse(peer.can_retry(0))
def connect_to_if_not_connected(self, peer: PeerId, now: int) -> None: """ Attempts to connect if it is not connected to the peer. """ import random if not peer.entrypoints: return if peer.id in self.connected_peers: return assert peer.id is not None if peer.can_retry(now): self.connect_to(random.choice(peer.entrypoints), peer)
def connect_to_if_not_connected(self, peer: PeerId, now: int) -> None: """ Attempts to connect if it is not connected to the peer. """ if not peer.entrypoints: # It makes no sense to keep storing peers that have disconnected and have no entrypoints # We will never be able to connect to them anymore and they will only keep spending memory # and other resources when used in APIs, so we are removing them here if peer.id not in self.connected_peers: self.peer_storage.remove(peer) return if peer.id in self.connected_peers: return assert peer.id is not None if peer.can_retry(now): self.connect_to(self.rng.choice(peer.entrypoints), peer)
def test_retry_logic(self): peer = PeerId('testnet') self.assertTrue(peer.can_retry(0)) retry_interval = peer.retry_interval peer.increment_retry_attempt(0) self.assertFalse(peer.can_retry(0)) self.assertFalse(peer.can_retry(retry_interval - 1)) self.assertTrue(peer.can_retry(retry_interval)) self.assertTrue(peer.can_retry(retry_interval + 1)) peer.increment_retry_attempt(0) self.assertFalse(peer.can_retry(retry_interval)) retry_interval *= settings.PEER_CONNECTION_RETRY_INTERVAL_MULTIPLIER self.assertFalse(peer.can_retry(retry_interval - 1)) self.assertTrue(peer.can_retry(retry_interval)) self.assertTrue(peer.can_retry(retry_interval)) # Retry until we reach max retry interval. while peer.retry_interval < settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL: peer.increment_retry_attempt(0) # We need to call it once more because peer.retry_interval is always one step behind. peer.increment_retry_attempt(0) # Confirm we are at the max retry interval. self.assertFalse( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL - 1)) self.assertTrue( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL)) self.assertTrue( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL + 1)) # It shouldn't change with another retry. peer.increment_retry_attempt(0) self.assertFalse( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL - 1)) self.assertTrue( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL)) self.assertTrue( peer.can_retry(settings.PEER_CONNECTION_RETRY_MAX_RETRY_INTERVAL + 1)) # Finally, reset it. peer.reset_retry_timestamp() self.assertTrue(peer.can_retry(0))