feat: Automatically retry up to three times when encountering a ServerDisconnectedError during API requests
This commit is contained in:
@@ -9,10 +9,10 @@ import re
|
|||||||
from typing import Any, Union, Dict
|
from typing import Any, Union, Dict
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import TCPConnector
|
from aiohttp import TCPConnector, ServerDisconnectedError
|
||||||
|
|
||||||
from ..exception import ResponseCodeException, ResponseException, NetworkException
|
|
||||||
from .Credential import Credential
|
from .Credential import Credential
|
||||||
|
from ..exception import ResponseCodeException, ResponseException, NetworkException
|
||||||
|
|
||||||
__session_pool = {}
|
__session_pool = {}
|
||||||
|
|
||||||
@@ -111,6 +111,8 @@ async def request(method: str,
|
|||||||
|
|
||||||
session = get_session()
|
session = get_session()
|
||||||
|
|
||||||
|
for i in range(3):
|
||||||
|
try:
|
||||||
async with session.request(**config) as resp:
|
async with session.request(**config) as resp:
|
||||||
|
|
||||||
# 检查状态码
|
# 检查状态码
|
||||||
@@ -149,6 +151,11 @@ async def request(method: str,
|
|||||||
raise ResponseCodeException(-1, "API 返回数据未含 code 字段", resp_data)
|
raise ResponseCodeException(-1, "API 返回数据未含 code 字段", resp_data)
|
||||||
|
|
||||||
if code != 0:
|
if code != 0:
|
||||||
|
# 加载错误,请稍后再试
|
||||||
|
if code == 4101131:
|
||||||
|
await asyncio.sleep(10)
|
||||||
|
continue
|
||||||
|
|
||||||
msg = resp_data.get('msg', None)
|
msg = resp_data.get('msg', None)
|
||||||
if msg is None:
|
if msg is None:
|
||||||
msg = resp_data.get('message', None)
|
msg = resp_data.get('message', None)
|
||||||
@@ -160,6 +167,9 @@ async def request(method: str,
|
|||||||
if real_data is None:
|
if real_data is None:
|
||||||
real_data = resp_data.get("result", None)
|
real_data = resp_data.get("result", None)
|
||||||
return real_data
|
return real_data
|
||||||
|
except ServerDisconnectedError:
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
def get_session() -> aiohttp.ClientSession:
|
def get_session() -> aiohttp.ClientSession:
|
||||||
|
|||||||
Reference in New Issue
Block a user