Skip to content

Commit 2b76696

Browse files
dt3310321tiedu
andauthored
opt retry logic (#149)
Co-authored-by: tiedu <[email protected]>
1 parent 2317d18 commit 2b76696

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

qcloud_cos/cos_client.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import json
1212
import xml.dom.minidom
1313
import xml.etree.ElementTree
14-
from requests import Request, Session
14+
from requests import Request, Session, ConnectionError, Timeout
1515
from datetime import datetime
1616
from six.moves.urllib.parse import quote, unquote, urlencode
1717
from six import text_type, binary_type
@@ -265,23 +265,16 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
265265
return res
266266
elif res.status_code < 500: # 4xx 不重试
267267
break
268+
else:
269+
if j < self._retry and client_can_retry(file_position, **kwargs):
270+
continue
271+
else:
272+
break
268273
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
269274
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
285278
raise CosClientError(str(e))
286279

287280
if not cos_request:

qcloud_cos/cos_comm.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,22 @@ def parse_bucket_canned_acl(result_acl):
437437
return "private"
438438

439439

440+
def client_can_retry(file_position, **kwargs):
441+
"""如果客户端请求中不包含data则可以重试,以及判断包含data的请求是否可以重试"""
442+
if 'data' not in kwargs:
443+
return True
444+
body = kwargs['data']
445+
if isinstance(body, text_type) or isinstance(body, binary_type):
446+
return True
447+
if file_position is not None and hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
448+
try:
449+
kwargs['data'].seek(file_position)
450+
return True
451+
except Exception as ioe:
452+
return False
453+
return False
454+
455+
440456
class CiDetectType():
441457
"""ci内容设备的类型设置,可与操作设多个"""
442458
PORN = 1

qcloud_cos/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = '5.1.9.2'
2+
__version__ = '5.1.9.3'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def long_description():
1616

1717
setup(
1818
name='cos-python-sdk-v5',
19-
version='1.9.2',
19+
version='1.9.3',
2020
url='https://www.qcloud.com/',
2121
license='MIT',
2222
author='tiedu, lewzylu, channingliu',

0 commit comments

Comments
 (0)