feature: use semaphore for scanning.
This commit is contained in:
@@ -32,6 +32,10 @@ class MinerNetwork:
|
|||||||
|
|
||||||
def __init__(self, hosts: List[ipaddress.IPv4Address]):
|
def __init__(self, hosts: List[ipaddress.IPv4Address]):
|
||||||
self.hosts = hosts
|
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):
|
def __len__(self):
|
||||||
return len(self.hosts)
|
return len(self.hosts)
|
||||||
@@ -153,8 +157,16 @@ class MinerNetwork:
|
|||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
yield None
|
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
|
@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:
|
try:
|
||||||
return await ping_and_get_miner(ip)
|
return await ping_and_get_miner(ip)
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ from httpx import AsyncHTTPTransport
|
|||||||
_settings = { # defaults
|
_settings = { # defaults
|
||||||
"network_ping_retries": 1,
|
"network_ping_retries": 1,
|
||||||
"network_ping_timeout": 3,
|
"network_ping_timeout": 3,
|
||||||
|
"network_scan_semaphore": None,
|
||||||
"factory_get_retries": 1,
|
"factory_get_retries": 1,
|
||||||
"factory_get_timeout": 3,
|
"factory_get_timeout": 3,
|
||||||
"get_data_retries": 1,
|
"get_data_retries": 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user