|
11 | 11 | import json |
12 | 12 | import xml.dom.minidom |
13 | 13 | import xml.etree.ElementTree |
14 | | -from requests import Request, Session |
| 14 | +from requests import Request, Session, ConnectionError, Timeout |
15 | 15 | from datetime import datetime |
16 | 16 | from six.moves.urllib.parse import quote, unquote, urlencode |
17 | 17 | from six import text_type, binary_type |
@@ -265,23 +265,16 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar |
265 | 265 | return res |
266 | 266 | elif res.status_code < 500: # 4xx 不重试 |
267 | 267 | break |
| 268 | + else: |
| 269 | + if j < self._retry and client_can_retry(file_position, **kwargs): |
| 270 | + continue |
| 271 | + else: |
| 272 | + break |
268 | 273 | except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误 |
269 | 274 | logger.exception('url:%s, retry_time:%d exception:%s' % (url, j, str(e))) |
270 | | - can_retry = False |
271 | | - if 'data' in kwargs: |
272 | | - body = kwargs['data'] |
273 | | - if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'): |
274 | | - can_retry = True |
275 | | - elif isinstance(body, text_type) or isinstance(body, binary_type): |
276 | | - can_retry = True |
277 | | - |
278 | | - if j < self._retry and can_retry: |
279 | | - if file_position is not None: |
280 | | - try: |
281 | | - kwargs['data'].seek(file_position) |
282 | | - except IOError as ioe: |
283 | | - raise CosClientError(str(ioe)) |
284 | | - continue |
| 275 | + if j < self._retry and (isinstance(e, ConnectionError) or isinstance(e, Timeout)): # 只重试网络错误 |
| 276 | + if client_can_retry(file_position, **kwargs): |
| 277 | + continue |
285 | 278 | raise CosClientError(str(e)) |
286 | 279 |
|
287 | 280 | if not cos_request: |
|
0 commit comments