@@ -107,7 +107,9 @@ async def check_session(self):
107
107
headers = self ._headers , connector = TCPConnector (limit = 500 , ssl = SSLContext ())
108
108
)
109
109
110
- async def request (self , route : Route , ** kwargs : Any ):
110
+ async def request (self , route : Route , retry_time : int = 0 , ** kwargs : Any ):
111
+ if retry_time > 2 :
112
+ return
111
113
# some checking if it's a JSON request
112
114
if "json" in kwargs :
113
115
json_ = kwargs ["json" ]
@@ -130,14 +132,21 @@ async def request(self, route: Route, **kwargs: Any):
130
132
route .is_sandbox = self .is_sandbox
131
133
_log .debug (f"[botpy] 请求头部: { self ._headers } , 请求方式: { route .method } , 请求url: { route .url } " )
132
134
_log .debug (self ._session )
133
- async with self ._session .request (
134
- method = route .method ,
135
- url = route .url ,
136
- timeout = (aiohttp .ClientTimeout (total = self .timeout )),
137
- ** kwargs ,
138
- ) as response :
139
- _log .debug (response )
140
- return await _handle_response (response )
135
+ try :
136
+ async with self ._session .request (
137
+ method = route .method ,
138
+ url = route .url ,
139
+ timeout = (aiohttp .ClientTimeout (total = self .timeout )),
140
+ ** kwargs ,
141
+ ) as response :
142
+ _log .debug (response )
143
+ return await _handle_response (response )
144
+ except asyncio .TimeoutError :
145
+ _log .debug ("session timeout retry" )
146
+ self ._session = aiohttp .ClientSession (
147
+ headers = self ._headers , connector = TCPConnector (limit = 500 , ssl = SSLContext ())
148
+ )
149
+ await self .request (route , retry_time + 1 , ** kwargs )
141
150
142
151
async def login (self , token : Token ) -> robot .Robot :
143
152
"""login后保存token和session"""
0 commit comments