Standardize async offloading behind shared executor helper
This commit is contained in:
@@ -4,7 +4,7 @@ Provides functions to list, read, and write files in the fail2ban
|
||||
configuration directory (``jail.d/``, ``filter.d/``, ``action.d/``).
|
||||
|
||||
All file operations are synchronous (wrapped in
|
||||
:func:`asyncio.get_event_loop().run_in_executor` by callers that need async
|
||||
:func:`app.utils.async_utils.run_blocking` by callers that need async
|
||||
behaviour) because the config files are small and infrequently touched — the
|
||||
overhead of async I/O is not warranted here.
|
||||
|
||||
@@ -16,6 +16,7 @@ traversal attacks.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from app.utils.async_utils import run_blocking
|
||||
import configparser
|
||||
import re
|
||||
from pathlib import Path
|
||||
@@ -291,7 +292,7 @@ async def list_jail_config_files(config_dir: str) -> JailConfigFilesResponse:
|
||||
log.info("jail_config_files_listed", count=len(files))
|
||||
return JailConfigFilesResponse(files=files, total=len(files))
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def get_jail_config_file(config_dir: str, filename: str) -> JailConfigFileContent:
|
||||
@@ -333,7 +334,7 @@ async def get_jail_config_file(config_dir: str, filename: str) -> JailConfigFile
|
||||
content=content,
|
||||
)
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def set_jail_config_enabled(
|
||||
@@ -386,7 +387,7 @@ async def set_jail_config_enabled(
|
||||
enabled=enabled,
|
||||
)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
async def create_jail_config_file(
|
||||
@@ -415,7 +416,7 @@ async def create_jail_config_file(
|
||||
log.info("jail_config_file_created", filename=filename)
|
||||
return filename
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def write_jail_config_file(
|
||||
@@ -458,7 +459,7 @@ async def write_jail_config_file(
|
||||
) from exc
|
||||
log.info("jail_config_file_written", filename=filename)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -624,7 +625,7 @@ async def list_filter_files(config_dir: str) -> ConfFilesResponse:
|
||||
log.info("filter_files_listed", count=result.total)
|
||||
return result
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def get_filter_file(config_dir: str, name: str) -> ConfFileContent:
|
||||
@@ -646,7 +647,7 @@ async def get_filter_file(config_dir: str, name: str) -> ConfFileContent:
|
||||
filter_d = _resolve_subdir(config_dir, "filter.d")
|
||||
return _read_conf_file(filter_d, name)
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def write_filter_file(
|
||||
@@ -672,7 +673,7 @@ async def write_filter_file(
|
||||
_write_conf_file(filter_d, name, req.content)
|
||||
log.info("filter_file_written", name=name)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
async def create_filter_file(
|
||||
@@ -701,7 +702,7 @@ async def create_filter_file(
|
||||
log.info("filter_file_created", filename=filename)
|
||||
return filename
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -728,7 +729,7 @@ async def list_action_files(config_dir: str) -> ConfFilesResponse:
|
||||
log.info("action_files_listed", count=result.total)
|
||||
return result
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def get_action_file(config_dir: str, name: str) -> ConfFileContent:
|
||||
@@ -750,7 +751,7 @@ async def get_action_file(config_dir: str, name: str) -> ConfFileContent:
|
||||
action_d = _resolve_subdir(config_dir, "action.d")
|
||||
return _read_conf_file(action_d, name)
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def write_action_file(
|
||||
@@ -776,7 +777,7 @@ async def write_action_file(
|
||||
_write_conf_file(action_d, name, req.content)
|
||||
log.info("action_file_written", name=name)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
async def create_action_file(
|
||||
@@ -805,7 +806,7 @@ async def create_action_file(
|
||||
log.info("action_file_created", filename=filename)
|
||||
return filename
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -840,7 +841,7 @@ async def get_parsed_filter_file(config_dir: str, name: str) -> FilterConfig:
|
||||
log.debug("filter_file_parsed", name=raw.name)
|
||||
return result
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def update_parsed_filter_file(
|
||||
@@ -879,7 +880,7 @@ async def update_parsed_filter_file(
|
||||
_write_conf_file(filter_d, name, new_content)
|
||||
log.info("filter_file_updated_parsed", name=name)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -910,7 +911,7 @@ async def get_parsed_action_file(config_dir: str, name: str) -> ActionConfig:
|
||||
log.debug("action_file_parsed", name=raw.name)
|
||||
return result
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def update_parsed_action_file(
|
||||
@@ -946,7 +947,7 @@ async def update_parsed_action_file(
|
||||
_write_conf_file(action_d, name, new_content)
|
||||
log.info("action_file_updated_parsed", name=name)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
|
||||
async def get_parsed_jail_file(config_dir: str, filename: str) -> JailFileConfig:
|
||||
@@ -972,7 +973,7 @@ async def get_parsed_jail_file(config_dir: str, filename: str) -> JailFileConfig
|
||||
log.debug("jail_file_parsed", filename=raw.filename)
|
||||
return result
|
||||
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
return await run_blocking( _do)
|
||||
|
||||
|
||||
async def update_parsed_jail_file(
|
||||
@@ -1008,4 +1009,4 @@ async def update_parsed_jail_file(
|
||||
_write_conf_file(jail_d, filename, new_content)
|
||||
log.info("jail_file_updated_parsed", filename=filename)
|
||||
|
||||
await asyncio.get_event_loop().run_in_executor(None, _do)
|
||||
await run_blocking( _do)
|
||||
|
||||
Reference in New Issue
Block a user