summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Dickinson <me@not.mn>2013-12-23 13:49:46 -0800
committerJohn Dickinson <me@not.mn>2013-12-23 13:53:18 -0800
commit5187fd313f3387644504e027f5191aa60475cbc2 (patch)
treea98aabfedbd71df06b49588a335b2c0c751cecef
parent3580bd90d09568ffb2ffeb56f6160684171001e0 (diff)
downloadpython-swiftclient-5187fd313f3387644504e027f5191aa60475cbc2.tar.gz
retry on ratelimit
Added a retry_on_ratelimit parameter to the Connection class so that ratelimited requests can be retried. DocImpact Change-Id: I2817a7ea0ed2d69a7659e80111fbd2c91a75d530
-rw-r--r--swiftclient/client.py10
-rw-r--r--tests/test_swiftclient.py18
2 files changed, 27 insertions, 1 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index dc457c6..65f2297 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -1035,7 +1035,8 @@ class Connection(object):
preauthurl=None, preauthtoken=None, snet=False,
starting_backoff=1, max_backoff=64, tenant_name=None,
os_options=None, auth_version="1", cacert=None,
- insecure=False, ssl_compression=True):
+ insecure=False, ssl_compression=True,
+ retry_on_ratelimit=False):
"""
:param authurl: authentication URL
:param user: user name to authenticate as
@@ -1061,6 +1062,10 @@ class Connection(object):
present an attempt to disable SSL compression
will be made. This may provide a performance
increase for https upload/download operations.
+ :param retry_on_ratelimit: by default, a ratelimited connection will
+ raise an exception to the caller. Setting
+ this parameter to True will cause a retry
+ after a backoff.
"""
self.authurl = authurl
self.user = user
@@ -1081,6 +1086,7 @@ class Connection(object):
self.insecure = insecure
self.ssl_compression = ssl_compression
self.auth_end_time = 0
+ self.retry_on_ratelimit = retry_on_ratelimit
def close(self):
if self.http_conn and type(self.http_conn) is tuple\
@@ -1154,6 +1160,8 @@ class Connection(object):
self.http_conn = None
elif 500 <= err.http_status <= 599:
pass
+ elif self.retry_on_ratelimit and err.http_status == 498:
+ pass
else:
logger.exception(err)
raise
diff --git a/tests/test_swiftclient.py b/tests/test_swiftclient.py
index 390a40e..6c680d6 100644
--- a/tests/test_swiftclient.py
+++ b/tests/test_swiftclient.py
@@ -713,6 +713,24 @@ class TestConnection(MockHttpTest):
self.assertRaises(c.ClientException, conn.head_account)
self.assertEqual(conn.attempts, conn.retries + 1)
+ def test_retry_on_ratelimit(self):
+ c.http_connection = self.fake_http_connection(498)
+
+ def quick_sleep(*args):
+ pass
+ c.sleep = quick_sleep
+
+ # test retries
+ conn = c.Connection('http://www.test.com', 'asdf', 'asdf',
+ retry_on_ratelimit=True)
+ self.assertRaises(c.ClientException, conn.head_account)
+ self.assertEqual(conn.attempts, conn.retries + 1)
+
+ # test default no-retry
+ conn = c.Connection('http://www.test.com', 'asdf', 'asdf')
+ self.assertRaises(c.ClientException, conn.head_account)
+ self.assertEqual(conn.attempts, 1)
+
def test_resp_read_on_server_error(self):
c.http_connection = self.fake_http_connection(500)
conn = c.Connection('http://www.test.com', 'asdf', 'asdf', retries=0)