From 5187fd313f3387644504e027f5191aa60475cbc2 Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Mon, 23 Dec 2013 13:49:46 -0800 Subject: [PATCH] retry on ratelimit Added a retry_on_ratelimit parameter to the Connection class so that ratelimited requests can be retried. DocImpact Change-Id: I2817a7ea0ed2d69a7659e80111fbd2c91a75d530 --- swiftclient/client.py | 10 +++++++++- tests/test_swiftclient.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/swiftclient/client.py b/swiftclient/client.py index dc457c6b..65f22972 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 390a40eb..6c680d69 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)