From 6d9d72247247d3781ed8fced9975f31e1f9e2715 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Tue, 2 Apr 2024 23:33:41 +0200 Subject: [PATCH] fix very slow HTTP queries httpx has a silly bug where it (very slowly) loads the ssl context even when it's not used at all. We can work around that by disabling certificates verification when the protocol is HTTP. A better fix would be to have a proper long-living scope for the client, but this requires large changes in astacus API implementation. --- astacus/common/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/astacus/common/utils.py b/astacus/common/utils.py index 6be9b657..f895ddbd 100644 --- a/astacus/common/utils.py +++ b/astacus/common/utils.py @@ -118,7 +118,12 @@ async def httpx_request( # TBD: may need to redact url in future, if we actually wind up # using passwords in urls here. logger.info("async-request %s %s by %s", method, url, caller) - async with httpx.AsyncClient() as client: + # httpx has a silly bug where it (very slowly) loads the ssl context even when it's not used at all. + # we can work around that by disabling certificates verification when the protocol is HTTP. + # A better fix would be to have a proper long-living scope for the client, but this requires large + # changes in astacus API implementation. + verify = not url.startswith("http://") + async with httpx.AsyncClient(verify=verify) as client: try: r = await client.request(method, url, timeout=timeout, **kw) if not r.is_error: