api: Only retry up to 10 times on connection errors.

(imported from commit f395370120d5e7a635eec7b27e4d5ed0c48b774d)
This commit is contained in:
Tim Abbott 2012-11-29 09:01:52 -05:00
parent 8683613e76
commit 70d9281f7a

View file

@ -58,6 +58,7 @@ class HumbugAPI(object):
request["email"] = self.email request["email"] = self.email
request["api-key"] = self.api_key request["api-key"] = self.api_key
request["client"] = self.client_name request["client"] = self.client_name
failures = 0
for (key, val) in request.iteritems(): for (key, val) in request.iteritems():
if not (isinstance(val, str) or isinstance(val, unicode)): if not (isinstance(val, str) or isinstance(val, unicode)):
@ -69,7 +70,7 @@ class HumbugAPI(object):
verify=True, timeout=55) verify=True, timeout=55)
# On 50x errors, try again after a short sleep # On 50x errors, try again after a short sleep
if str(res.status_code).startswith('5') and self.retry_on_errors: if str(res.status_code).startswith('5') and self.retry_on_errors and failures < 10:
if self.verbose: if self.verbose:
if not had_error_retry: if not had_error_retry:
sys.stdout.write("connection error %s -- retrying." % (res.status_code,)) sys.stdout.write("connection error %s -- retrying." % (res.status_code,))
@ -79,6 +80,7 @@ class HumbugAPI(object):
sys.stdout.write(".") sys.stdout.write(".")
sys.stdout.flush() sys.stdout.flush()
time.sleep(1) time.sleep(1)
failures += 1
continue continue
except (requests.exceptions.Timeout, requests.exceptions.SSLError) as e: except (requests.exceptions.Timeout, requests.exceptions.SSLError) as e:
# Timeouts are either a Timeout or an SSLError; we # Timeouts are either a Timeout or an SSLError; we
@ -95,7 +97,7 @@ class HumbugAPI(object):
return {'msg': "Connection error:\n%s" % traceback.format_exc(), return {'msg': "Connection error:\n%s" % traceback.format_exc(),
"result": "connection-error"} "result": "connection-error"}
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
if self.retry_on_errors: if self.retry_on_errors and failures < 10:
if self.verbose: if self.verbose:
if not had_error_retry: if not had_error_retry:
sys.stdout.write("connection error -- retrying.") sys.stdout.write("connection error -- retrying.")
@ -105,6 +107,7 @@ class HumbugAPI(object):
sys.stdout.write(".") sys.stdout.write(".")
sys.stdout.flush() sys.stdout.flush()
time.sleep(1) time.sleep(1)
failures += 1
continue continue
return {'msg': "Connection error:\n%s" % traceback.format_exc(), return {'msg': "Connection error:\n%s" % traceback.format_exc(),
"result": "connection-error"} "result": "connection-error"}