@@ -30,17 +30,15 @@ logger = logger.getChild('searx.http.client')
3030LOOP = None
3131SSLCONTEXTS = {}
3232TRANSPORT_KWARGS = {
33− 'backend': 'asyncio',
3433 'trust_env': False,
3534}
3635
3736
38−async def close_connections_for_url(connection_pool: httpcore.AsyncConnectionPool, url: httpcore._utils.URL):
39− origin = httpcore._utils.url_to_origin(url)
40− logger.debug('Drop connections for %r', origin)
41− connections_to_close = connection_pool._connections_for_origin(origin)
37+async def close_connections_for_url(connection_pool: httpcore.AsyncConnectionPool, url: httpx._models.URL):
38+ logger.debug('Drop connections for %r', url.host)
39+ connections_to_close = [conn for conn in connection_pool._pool if conn._origin == url.host]
4240 for connection in connections_to_close:
43− await connection_pool._remove_from_pool(connection)
41+ connection_pool._pool.remove(connection)
4442 try:
4543 await connection.aclose()
4644 except httpx.NetworkError as e:
@@ -76,12 +74,12 @@ class AsyncProxyTransportFixed(AsyncProxyTransport):
7674 Note: AsyncProxyTransport inherit from AsyncConnectionPool
7775 """
7876
79− async def handle_async_request(self, method, url, headers=None, stream=None, extensions=None):
77+ async def handle_async_request(self, request: httpx.Request):
8078 retry = 2
8179 while retry > 0:
8280 retry -= 1
8381 try:
84− return await super().handle_async_request(method, url, headers, stream, extensions)
82+ return await super().handle_async_request(request)
8583 except (ProxyConnectionError, ProxyTimeoutError, ProxyError) as e:
8684 raise httpx.ProxyError(e)
8785 except OSError as e:
@@ -89,25 +87,25 @@ class AsyncProxyTransportFixed(AsyncProxyTransport):
8987 raise httpx.NetworkError(e)
9088 except httpx.RemoteProtocolError as e:
9189 # in case of httpx.RemoteProtocolError: Server disconnected
92− await close_connections_for_url(self, url)
90+ await close_connections_for_url(self, request.url)
9391 logger.warning('httpx.RemoteProtocolError: retry', exc_info=e)
9492 # retry
9593 except (httpx.NetworkError, httpx.ProtocolError) as e:
9694 # httpx.WriteError on HTTP/2 connection leaves a new opened stream
9795 # then each new request creates a new stream and raise the same WriteError
98− await close_connections_for_url(self, url)
96+ await close_connections_for_url(self, request.url)
9997 raise e
10098
10199
102100class AsyncHTTPTransportFixed(httpx.AsyncHTTPTransport):
103101 """Fix httpx.AsyncHTTPTransport"""
104102
105− async def handle_async_request(self, method, url, headers=None, stream=None, extensions=None):
103+ async def handle_async_request(self, request: httpx.Request):
106104 retry = 2
107105 while retry > 0:
108106 retry -= 1
109107 try:
110− return await super().handle_async_request(method, url, headers, stream, extensions)
108+ return await super().handle_async_request(request)
111109 except OSError as e:
112110 # socket.gaierror when DNS resolution fails
113111 raise httpx.ConnectError(e)
@@ -115,16 +113,16 @@ class AsyncHTTPTransportFixed(httpx.AsyncHTTPTransport):
115113 # httpx.CloseError: [Errno 104] Connection reset by peer
116114 # raised by _keepalive_sweep()
117115 # from https://github.com/encode/httpcore/blob/4b662b5c42378a61e54d673b4c949420102379f5/httpcore/_backends/asyncio.py#L198 # noqa
118− await close_connections_for_url(self._pool, url)
116+ await close_connections_for_url(self._pool, request.url)
119117 logger.warning('httpx.CloseError: retry', exc_info=e)
120118 # retry
121119 except httpx.RemoteProtocolError as e:
122120 # in case of httpx.RemoteProtocolError: Server disconnected
123− await close_connections_for_url(self._pool, url)
121+ await close_connections_for_url(self._pool, request.url)
124122 logger.warning('httpx.RemoteProtocolError: retry', exc_info=e)
125123 # retry
126124 except (httpx.ProtocolError, httpx.NetworkError) as e:
127− await close_connections_for_url(self._pool, url)
125+ await close_connections_for_url(self._pool, request.url)
128126 raise e
129127
130128