run refresh_netsets provider fetches concurrently with asyncio.gather

This commit is contained in:
Andrew Pimenov 2026-08-09 16:12:15 +03:00
parent 6810c0f753
commit 52c0647e77

View file

@ -75,26 +75,49 @@ async def refresh_netsets(
cache_dir: Path, cache_dir: Path,
) -> NetsetIndex: ) -> NetsetIndex:
directory = Path(cache_dir) / "firehol" directory = Path(cache_dir) / "firehol"
def setup_dir() -> None:
directory.mkdir(parents=True, exist_ok=True) directory.mkdir(parents=True, exist_ok=True)
await asyncio.to_thread(setup_dir)
index = NetsetIndex() index = NetsetIndex()
for url in providers.firehol_netsets:
async def process_one(url: str) -> tuple[str, list[str] | None]:
name = url.rsplit("/", 1)[-1].removesuffix(".netset") name = url.rsplit("/", 1)[-1].removesuffix(".netset")
path = directory / f"{name}.netset" path = directory / f"{name}.netset"
fresh = (
def check_fresh() -> bool:
return (
path.exists() path.exists()
and (time.time() - os.path.getmtime(path)) < providers.firehol_refresh_hours * 3600 and (time.time() - os.path.getmtime(path)) < providers.firehol_refresh_hours * 3600
) )
fresh = await asyncio.to_thread(check_fresh)
if not fresh: if not fresh:
try: try:
response = await client.get(url) response = await client.get(url)
response.raise_for_status() response.raise_for_status()
def write_file() -> None:
tmp = path.with_suffix(".netset.tmp") tmp = path.with_suffix(".netset.tmp")
tmp.write_text(response.text, encoding="utf-8") tmp.write_text(response.text, encoding="utf-8")
os.replace(tmp, path) os.replace(tmp, path)
await asyncio.to_thread(write_file)
except httpx.HTTPError: except httpx.HTTPError:
if not path.exists(): if not await asyncio.to_thread(path.exists):
continue return name, None
index.add(name, parse_netset(path.read_text(encoding="utf-8")))
def read_and_parse() -> list[str]:
return parse_netset(path.read_text(encoding="utf-8"))
parsed = await asyncio.to_thread(read_and_parse)
return name, parsed
results = await asyncio.gather(*(process_one(url) for url in providers.firehol_netsets))
for name, parsed in results:
if parsed is not None:
index.add(name, parsed)
return index return index