Skip to content

Commit 22c18db

Browse files
Improve retry handling, config, test suite
Fixes #275 and improves test suite. Better documents the effect of WAIT_ON_RATE_LIMIT argument.
1 parent 4529db2 commit 22c18db

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

meraki/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
REQUESTS_PROXY = ""
2323

2424
# Retry if 429 rate limit error encountered?
25+
# Please note, setting to False means your application will not retry upon a 429. Not intended for production apps.
2526
WAIT_ON_RATE_LIMIT = True
2627

2728
# Nginx 429 retry wait time

meraki/rest_session.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,18 @@ def request(self, metadata, method, url, **kwargs):
207207

208208
# Rate limit 429 errors
209209
elif status == 429:
210-
if 'Retry-After' in response.headers:
211-
wait = int(response.headers['Retry-After'])
210+
# Retry if 429 retries are enabled and there are retries left
211+
if self._wait_on_rate_limit and retries > 0:
212+
if 'Retry-After' in response.headers:
213+
wait = int(response.headers['Retry-After'])
214+
else:
215+
wait = random.randint(1, self._nginx_429_retry_wait_time)
216+
if self._logger:
217+
self._logger.warning(f'{tag}, {operation} - {status} {reason}, retrying in {wait} seconds')
218+
time.sleep(wait)
219+
retries -= 1
220+
# We're either out of retries or the client told us not to retry
212221
else:
213-
wait = random.randint(1, self._nginx_429_retry_wait_time)
214-
if self._logger:
215-
self._logger.warning(f'{tag}, {operation} - {status} {reason}, retrying in {wait} seconds')
216-
time.sleep(wait)
217-
retries -= 1
218-
if retries == 0:
219222
raise APIError(metadata, response)
220223

221224
# 5XX errors
@@ -249,7 +252,7 @@ def handle_4xx_errors(self, metadata, operation, reason, response, retries, stat
249252
action_batch_errors = [error for error in message['errors'] if action_batch_concurrency_error_text in error]
250253

251254
if network_deletion_errors:
252-
wait = random.randint(30, self._network_delete_retry_wait_time)
255+
wait = random.randint(10, self._network_delete_retry_wait_time)
253256
if self._logger:
254257
self._logger.warning(f'{tag}, {operation} - {status} {reason}, retrying in {wait} seconds')
255258
time.sleep(wait)

tests/test_dashboard_api_python_library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def api_key(pytestconfig):
1414

1515
@pytest.fixture(scope='session')
1616
def dashboard(api_key):
17-
return meraki.DashboardAPI(api_key, suppress_logging=True, network_delete_retry_wait_time=600)
17+
return meraki.DashboardAPI(api_key, suppress_logging=True, network_delete_retry_wait_time=600, maximum_retries=50)
1818

1919

2020
@pytest.fixture(scope='session')

0 commit comments

Comments
 (0)