99import logging
1010import uuid
1111
12+ import backoff
13+
1214from tigeropen import __VERSION__
1315from tigeropen .common .consts import OPEN_API_SERVICE_VERSION , THREAD_LOCAL
1416from tigeropen .common .consts .params import P_TIMESTAMP , P_TIGER_ID , P_METHOD , P_CHARSET , P_VERSION , P_SIGN_TYPE , \
1517 P_DEVICE_ID , P_NOTIFY_URL , COMMON_PARAM_KEYS , P_SIGN
16- from tigeropen .common .consts .service_types import USER_LICENSE
18+ from tigeropen .common .consts .service_types import USER_LICENSE , PLACE_ORDER , CANCEL_ORDER , MODIFY_ORDER
1719from tigeropen .common .exceptions import ResponseException , RequestException
1820from tigeropen .common .request import OpenApiRequest
1921from tigeropen .common .response import TigerResponse
@@ -28,6 +30,7 @@ def get_mac_address():
2830 return ':' .join (("%012x" % uuid .getnode ())[i :i + 2 ] for i in range (0 , 12 , 2 ))
2931
3032LOG_FORMATTER = logging .Formatter ('%(asctime)s %(name)s %(levelname)s: %(message)s' )
33+ SKIP_RETRY_SERVICES = {PLACE_ORDER , CANCEL_ORDER , MODIFY_ORDER }
3134
3235
3336class TigerOpenClient :
@@ -153,20 +156,32 @@ def __parse_response(self, response_str, timestamp=None):
153156
154157 return response_content
155158
159+ def _get_retry_deco (self , service ):
160+ if service not in SKIP_RETRY_SERVICES and self .__config .retry_max_tries > 0 :
161+ return backoff .on_exception (backoff .fibo ,
162+ (RequestException , ResponseException ),
163+ max_time = self .__config .retry_max_time ,
164+ max_tries = self .__config .retry_max_tries ,
165+ jitter = None )
166+ return None
167+
156168 """
157169 执行接口请求
158170 """
159-
160171 def execute (self , request , url = None ):
161172 if url is None :
162173 url = self .__config .server_url
163174 THREAD_LOCAL .uuid = str (uuid .uuid1 ())
164175 query_string = None
165176 params = self .__prepare_request (request , url )
166177
167- response = do_post (url , query_string , self .__headers , params , self .__config .timeout ,
168- self .__config .charset )
169-
178+ retry_deco = self ._get_retry_deco (request ._method )
179+ if retry_deco is not None :
180+ response = retry_deco (do_post )(url , query_string , self .__headers , params , self .__config .timeout ,
181+ self .__config .charset )
182+ else :
183+ response = do_post (url , query_string , self .__headers , params , self .__config .timeout ,
184+ self .__config .charset )
170185 return self .__parse_response (response , params .get ('timestamp' ))
171186
172187 def query_license (self ):
0 commit comments