async def _add_peers_from_backend(self, backend: BasePeerBackend) -> None: available_slots = self.max_peers - len(self) try: connected_remotes = { peer.remote for peer in self.connected_nodes.values() } candidates = await self.wait( backend.get_peer_candidates( num_requested=available_slots, connected_remotes=connected_remotes, ), timeout=REQUEST_PEER_CANDIDATE_TIMEOUT, ) except asyncio.TimeoutError: self.logger.warning("PeerCandidateRequest timed out to backend %s", backend) return else: self.logger.debug2( "Got candidates from backend %s (%s)", backend, candidates, ) if candidates: await self.connect_to_nodes(iter(candidates))
async def _add_peers_from_backend( self, backend: BasePeerBackend, should_skip_fn: Callable[[Tuple[NodeID, ...], NodeAPI], bool]) -> int: connected_node_ids = { peer.remote.id for peer in self.connected_nodes.values() } # Only ask for random bootnodes if we're not connected to any peers. if isinstance(backend, BootnodesPeerBackend) and len(connected_node_ids) > 0: return 0 try: blacklisted_node_ids = await asyncio.wait_for( self.connection_tracker.get_blacklisted(), timeout=1) except asyncio.TimeoutError: self.logger.warning( "Timed out getting blacklisted peers from connection tracker, pausing peer " "addition until we can get that.") return 0 skip_list = connected_node_ids.union(blacklisted_node_ids) should_skip_fn = functools.partial(should_skip_fn, skip_list) # Request a large batch on every iteration as that will effectively push DiscoveryService # to trigger new peer lookups in order to find enough compatible peers to fulfill our # request. There's probably some room for experimentation here in order to find an optimal # value. max_candidates = self.available_slots * OVER_PROVISION_MISSING_PEERS try: candidates = await asyncio.wait_for( backend.get_peer_candidates( max_candidates=max_candidates, should_skip_fn=should_skip_fn, ), timeout=REQUEST_PEER_CANDIDATE_TIMEOUT, ) except asyncio.TimeoutError: self.logger.warning("PeerCandidateRequest timed out to backend %s", backend) return 0 else: self.logger.debug("Got %d peer candidates from backend %s", len(candidates), backend) if candidates: await self.connect_to_nodes(candidates) return len(candidates)
async def _add_peers_from_backend( self, backend: BasePeerBackend, should_skip_fn: Callable[[Tuple[NodeID, ...], NodeAPI], bool]) -> int: connected_node_ids = { peer.remote.id for peer in self.connected_nodes.values() } # Only ask for random bootnodes if we're not connected to any peers. if isinstance(backend, BootnodesPeerBackend) and len(connected_node_ids) > 0: return 0 try: blacklisted_node_ids = await self.wait( self.connection_tracker.get_blacklisted(), timeout=1) except asyncio.TimeoutError: self.logger.warning( "Timed out getting blacklisted peers from connection tracker, pausing peer " "addition until we can get that.") return 0 skip_list = connected_node_ids.union(blacklisted_node_ids) should_skip_fn = functools.partial(should_skip_fn, skip_list) try: candidates = await self.wait( backend.get_peer_candidates( max_candidates=self.available_slots, should_skip_fn=should_skip_fn, ), timeout=REQUEST_PEER_CANDIDATE_TIMEOUT, ) except asyncio.TimeoutError: self.logger.warning("PeerCandidateRequest timed out to backend %s", backend) return 0 else: self.logger.debug2( "Got candidates from backend %s (%s)", backend, candidates, ) if candidates: await self.connect_to_nodes(iter(candidates)) return len(candidates)