diff --git a/coco/core.py b/coco/core.py index 09ca9943..270ee60b 100644 --- a/coco/core.py +++ b/coco/core.py @@ -85,10 +85,17 @@ def __init__(self, conf, reset=False, check_config=False): raise ConfigError( f"Failed parsing value 'timeout' ({self.config['timeout']})." ) from e + try: + dns_cache_ttl = str2total_seconds(self.config["dns_cache_ttl"]) + except Exception as e: + raise ConfigError( + f"Failed parsing value 'dns_cache_ttl' ({self.config['dns_cache_ttl']})." + ) from e self.forwarder = RequestForwarder( self.blocklist_path, timeout, debug_connections=self.config["debug_connections"], + dns_cache_ttl=dns_cache_ttl ) self.forwarder.set_session_limit(self.config["session_limit"]) for group, hosts in self.groups.items(): diff --git a/coco/request_forwarder.py b/coco/request_forwarder.py index 644610df..f2894b13 100644 --- a/coco/request_forwarder.py +++ b/coco/request_forwarder.py @@ -196,7 +196,7 @@ class RequestForwarder: """ def __init__( - self, blocklist_path: os.PathLike, timeout: int, debug_connections: bool = False + self, blocklist_path: os.PathLike, timeout: int, debug_connections: bool = False, dns_cache_ttl: int = 10 ): self._endpoints = dict() self._groups = dict() @@ -210,6 +210,7 @@ def __init__( self.queue_wait_time = None self.response_time = None self._debug_connections = debug_connections + self._connector = aiohttp.TCPConnector(limit=0, ttl_dns_cache=dns_cache_ttl) def set_session_limit(self, session_limit): """ @@ -421,9 +422,8 @@ async def external(self, name, request, hosts, method, params=None, timeout=None if timeout is None: timeout = self.timeout - connector = aiohttp.TCPConnector(limit=0) async with aiohttp.ClientSession( - connector=connector, + connector=self._connector, trace_configs=([_trace_config()] if self._debug_connections else None), ) as session, TaskPool(self.session_limit) as tasks: for host in hosts: diff --git a/coco/test/coco_runner.py b/coco/test/coco_runner.py index 9223808e..1608d41a 100644 --- a/coco/test/coco_runner.py +++ b/coco/test/coco_runner.py @@ -18,6 +18,7 @@ "host": "localhost", "port": 12055, "log_level": "DEBUG", + "dns_cache_ttl": 60, "blocklist_path": str(BLOCKLIST_PATH), "storage_path": STATE_DIR.name, }