🐛 Fix post timeout error

This commit is contained in:
xtaodada 2023-04-25 23:00:44 +08:00
parent 6efddb364a
commit d51e5c64cc
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 13 additions and 3 deletions

View File

@ -6,10 +6,20 @@ import httpx
__all__ = ("HTTPXRequest",) __all__ = ("HTTPXRequest",)
from core.config import config
timeout = httpx.Timeout(
timeout=config.timeout,
read=config.read_timeout,
write=config.write_timeout,
connect=config.connect_timeout,
pool=config.pool_timeout,
)
class HTTPXRequest(AbstractAsyncContextManager): class HTTPXRequest(AbstractAsyncContextManager):
def __init__(self, *args, headers=None, **kwargs): def __init__(self, *args, headers=None, **kwargs):
self._client = httpx.AsyncClient(headers=headers, *args, **kwargs) self._client = httpx.AsyncClient(headers=headers, timeout=timeout, *args, **kwargs)
async def __aenter__(self): async def __aenter__(self):
try: try:
@ -26,7 +36,7 @@ class HTTPXRequest(AbstractAsyncContextManager):
async def initialize(self): async def initialize(self):
if self._client.is_closed: if self._client.is_closed:
self._client = httpx.AsyncClient() self._client = httpx.AsyncClient(timeout=timeout)
async def shutdown(self): async def shutdown(self):
if self._client.is_closed: if self._client.is_closed:

View File

@ -123,7 +123,7 @@ class Hyperion:
_, file_extension = os.path.splitext(filename) _, file_extension = os.path.splitext(filename)
is_image = bool(file_extension in ".jpg" or file_extension in ".png") is_image = bool(file_extension in ".jpg" or file_extension in ".png")
response = await self.client.get( response = await self.client.get(
url, params=self.get_images_params(resize=2000) if is_image else None, timeout=10, de_json=False url, params=self.get_images_params(resize=2000) if is_image else None, de_json=False
) )
return ArtworkImage( return ArtworkImage(
art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content