X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/19ae770973482257117fe8ded5619c3018c4b60f..04efddf61ee4a0e5c65a72a538fe3f026ae94e8e:/sdk/python/arvados/retry.py diff --git a/sdk/python/arvados/retry.py b/sdk/python/arvados/retry.py index d8f5317d2c..2f2724333c 100644 --- a/sdk/python/arvados/retry.py +++ b/sdk/python/arvados/retry.py @@ -1,5 +1,5 @@ -#!/usr/bin/env python - +from builtins import range +from builtins import object import functools import inspect import pycurl @@ -9,7 +9,7 @@ from collections import deque import arvados.errors -_HTTP_SUCCESSES = set(xrange(200, 300)) +_HTTP_SUCCESSES = set(range(200, 300)) _HTTP_CAN_RETRY = set([408, 409, 422, 423, 500, 502, 503, 504]) class RetryLoop(object): @@ -31,7 +31,8 @@ class RetryLoop(object): return loop.last_result() """ def __init__(self, num_retries, success_check=lambda r: True, - backoff_start=0, backoff_growth=2, save_results=1): + backoff_start=0, backoff_growth=2, save_results=1, + max_wait=60): """Construct a new RetryLoop. Arguments: @@ -50,11 +51,13 @@ class RetryLoop(object): * save_results: Specify a number to save the last N results that the loop recorded. These records are available through the results attribute, oldest first. Default 1. + * max_wait: Maximum number of seconds to wait between retries. """ self.tries_left = num_retries + 1 self.check_result = success_check self.backoff_wait = backoff_start self.backoff_growth = backoff_growth + self.max_wait = max_wait self.next_start_time = 0 self.results = deque(maxlen=save_results) self._running = None @@ -66,7 +69,7 @@ class RetryLoop(object): def running(self): return self._running and (self._success is None) - def next(self): + def __next__(self): if self._running is None: self._running = True if (self.tries_left < 1) or not self.running(): @@ -76,6 +79,8 @@ class RetryLoop(object): wait_time = max(0, self.next_start_time - time.time()) time.sleep(wait_time) self.backoff_wait *= self.backoff_growth + if self.backoff_wait > self.max_wait: + self.backoff_wait = self.max_wait self.next_start_time = time.time() + self.backoff_wait self.tries_left -= 1 return self.tries_left