feature: use semaphore for scanning.

This commit is contained in:
Upstream Data
2024-05-06 14:43:03 -06:00
parent 79e247c0cf
commit dd205c0f06
2 changed files with 14 additions and 1 deletions

View File

@@ -32,6 +32,10 @@ class MinerNetwork:
def __init__(self, hosts: List[ipaddress.IPv4Address]):
self.hosts = hosts
semaphore_limit = settings.get("network_scan_semaphore", 255)
if semaphore_limit is None:
semaphore_limit = 255
self.semaphore = asyncio.Semaphore(semaphore_limit)
def __len__(self):
return len(self.hosts)
@@ -153,8 +157,16 @@ class MinerNetwork:
except TimeoutError:
yield None
async def ping_and_get_miner(
self, ip: ipaddress.ip_address
) -> Union[None, AnyMiner]:
if settings.get("network_scan_semaphore") is None:
return await self._ping_and_get_miner(ip)
async with self.semaphore:
return await self._ping_and_get_miner(ip)
@staticmethod
async def ping_and_get_miner(ip: ipaddress.ip_address) -> Union[None, AnyMiner]:
async def _ping_and_get_miner(ip: ipaddress.ip_address) -> Union[None, AnyMiner]:
try:
return await ping_and_get_miner(ip)
except ConnectionRefusedError:

View File

@@ -24,6 +24,7 @@ from httpx import AsyncHTTPTransport
_settings = { # defaults
"network_ping_retries": 1,
"network_ping_timeout": 3,
"network_scan_semaphore": None,
"factory_get_retries": 1,
"factory_get_timeout": 3,
"get_data_retries": 1,