Skip to content

Commit dda1fd9

Browse files
committed
refactor: RequestUtils 使用全局唯一 HTTP 客户端
1 parent 2f6426b commit dda1fd9

1 file changed

Lines changed: 3 additions & 16 deletions

File tree

app/utils/http.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
from collections.abc import Coroutine
66
from tempfile import TemporaryDirectory
77
from shutil import copy
8-
from weakref import WeakSet
98

109
from httpx import AsyncClient, Client, Response, TimeoutException
1110
from aiofile import async_open
1211

1312
from app.core import settings, logger
14-
from app.utils.url import URLUtils
1513
from app.utils.retry import Retry
1614

1715

@@ -340,28 +338,17 @@ class RequestUtils:
340338
支持同步和异步请求
341339
"""
342340

343-
__clients: dict[str, HTTPClient] = {}
344-
__client_list: WeakSet[HTTPClient] = WeakSet()
341+
__client = HTTPClient
345342

346343
@classmethod
347-
def get_client(cls, url: str = "") -> HTTPClient:
344+
def get_client(cls, *_, **__) -> HTTPClient:
348345
"""
349346
获取 HTTP 客户端
350347
351348
:param url: 请求的 URL
352349
:return: HTTP 客户端
353350
"""
354-
355-
if url:
356-
_, domain, port = URLUtils.get_resolve_url(url)
357-
key = f"{domain}:{port}"
358-
if key not in cls.__clients:
359-
cls.__clients[key] = HTTPClient()
360-
return cls.__clients[key]
361-
362-
client = HTTPClient()
363-
cls.__client_list.add(client)
364-
return client
351+
return cls.client
365352

366353
@overload
367354
@classmethod

0 commit comments

Comments
 (0)