From 89fe1c6c1a55e9df2c539cc0627a70b66329c00f Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Fri, 25 Aug 2023 14:06:40 +0000 Subject: [PATCH] [mod] searx.network: memory optimization Avoid to create a SSLContext in AsyncHTTPTransportNoHttp See: * https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_transports/default.py#L271 * https://github.com/encode/httpx/issues/2298 --- searx/network/client.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/searx/network/client.py b/searx/network/client.py index ed1796515..23826c75d 100644 --- a/searx/network/client.py +++ b/searx/network/client.py @@ -61,11 +61,40 @@ def get_sslcontexts(proxy_url=None, cert=None, verify=True, trust_env=True, http class AsyncHTTPTransportNoHttp(httpx.AsyncHTTPTransport): - """Block HTTP request""" + """Block HTTP request + + The constructor is blank because httpx.AsyncHTTPTransport.__init__ creates an SSLContext unconditionally: + https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_transports/default.py#L271 + + Each SSLContext consumes more than 500kb of memory, since there is about one network per engine. + + In consequence, this class overrides all public methods + + For reference: https://github.com/encode/httpx/issues/2298 + """ + + def __init__(self, *args, **kwargs): + # pylint: disable=super-init-not-called + # this on purpose if the base class is not called + pass async def handle_async_request(self, request): raise httpx.UnsupportedProtocol('HTTP protocol is disabled') + async def aclose(self) -> None: + pass + + async def __aenter__(self): + return self + + async def __aexit__( + self, + exc_type=None, + exc_value=None, + traceback=None, + ) -> None: + pass + class AsyncProxyTransportFixed(AsyncProxyTransport): """Fix httpx_socks.AsyncProxyTransport