Path: blob/master/venv/Lib/site-packages/pip/_vendor/retrying.py
811 views
## Copyright 2013-2014 Ray Holder1##2## Licensed under the Apache License, Version 2.0 (the "License");3## you may not use this file except in compliance with the License.4## You may obtain a copy of the License at5##6## http://www.apache.org/licenses/LICENSE-2.07##8## Unless required by applicable law or agreed to in writing, software9## distributed under the License is distributed on an "AS IS" BASIS,10## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11## See the License for the specific language governing permissions and12## limitations under the License.1314import random15from pip._vendor import six16import sys17import time18import traceback192021# sys.maxint / 2, since Python 3.2 doesn't have a sys.maxint...22MAX_WAIT = 1073741823232425def retry(*dargs, **dkw):26"""27Decorator function that instantiates the Retrying object28@param *dargs: positional arguments passed to Retrying object29@param **dkw: keyword arguments passed to the Retrying object30"""31# support both @retry and @retry() as valid syntax32if len(dargs) == 1 and callable(dargs[0]):33def wrap_simple(f):3435@six.wraps(f)36def wrapped_f(*args, **kw):37return Retrying().call(f, *args, **kw)3839return wrapped_f4041return wrap_simple(dargs[0])4243else:44def wrap(f):4546@six.wraps(f)47def wrapped_f(*args, **kw):48return Retrying(*dargs, **dkw).call(f, *args, **kw)4950return wrapped_f5152return wrap535455class Retrying(object):5657def __init__(self,58stop=None, wait=None,59stop_max_attempt_number=None,60stop_max_delay=None,61wait_fixed=None,62wait_random_min=None, wait_random_max=None,63wait_incrementing_start=None, wait_incrementing_increment=None,64wait_exponential_multiplier=None, wait_exponential_max=None,65retry_on_exception=None,66retry_on_result=None,67wrap_exception=False,68stop_func=None,69wait_func=None,70wait_jitter_max=None):7172self._stop_max_attempt_number = 5 if stop_max_attempt_number is None else stop_max_attempt_number73self._stop_max_delay = 100 if stop_max_delay is None else stop_max_delay74self._wait_fixed = 1000 if wait_fixed is None else wait_fixed75self._wait_random_min = 0 if wait_random_min is None else wait_random_min76self._wait_random_max = 1000 if wait_random_max is None else wait_random_max77self._wait_incrementing_start = 0 if wait_incrementing_start is None else wait_incrementing_start78self._wait_incrementing_increment = 100 if wait_incrementing_increment is None else wait_incrementing_increment79self._wait_exponential_multiplier = 1 if wait_exponential_multiplier is None else wait_exponential_multiplier80self._wait_exponential_max = MAX_WAIT if wait_exponential_max is None else wait_exponential_max81self._wait_jitter_max = 0 if wait_jitter_max is None else wait_jitter_max8283# TODO add chaining of stop behaviors84# stop behavior85stop_funcs = []86if stop_max_attempt_number is not None:87stop_funcs.append(self.stop_after_attempt)8889if stop_max_delay is not None:90stop_funcs.append(self.stop_after_delay)9192if stop_func is not None:93self.stop = stop_func9495elif stop is None:96self.stop = lambda attempts, delay: any(f(attempts, delay) for f in stop_funcs)9798else:99self.stop = getattr(self, stop)100101# TODO add chaining of wait behaviors102# wait behavior103wait_funcs = [lambda *args, **kwargs: 0]104if wait_fixed is not None:105wait_funcs.append(self.fixed_sleep)106107if wait_random_min is not None or wait_random_max is not None:108wait_funcs.append(self.random_sleep)109110if wait_incrementing_start is not None or wait_incrementing_increment is not None:111wait_funcs.append(self.incrementing_sleep)112113if wait_exponential_multiplier is not None or wait_exponential_max is not None:114wait_funcs.append(self.exponential_sleep)115116if wait_func is not None:117self.wait = wait_func118119elif wait is None:120self.wait = lambda attempts, delay: max(f(attempts, delay) for f in wait_funcs)121122else:123self.wait = getattr(self, wait)124125# retry on exception filter126if retry_on_exception is None:127self._retry_on_exception = self.always_reject128else:129self._retry_on_exception = retry_on_exception130131# TODO simplify retrying by Exception types132# retry on result filter133if retry_on_result is None:134self._retry_on_result = self.never_reject135else:136self._retry_on_result = retry_on_result137138self._wrap_exception = wrap_exception139140def stop_after_attempt(self, previous_attempt_number, delay_since_first_attempt_ms):141"""Stop after the previous attempt >= stop_max_attempt_number."""142return previous_attempt_number >= self._stop_max_attempt_number143144def stop_after_delay(self, previous_attempt_number, delay_since_first_attempt_ms):145"""Stop after the time from the first attempt >= stop_max_delay."""146return delay_since_first_attempt_ms >= self._stop_max_delay147148def no_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):149"""Don't sleep at all before retrying."""150return 0151152def fixed_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):153"""Sleep a fixed amount of time between each retry."""154return self._wait_fixed155156def random_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):157"""Sleep a random amount of time between wait_random_min and wait_random_max"""158return random.randint(self._wait_random_min, self._wait_random_max)159160def incrementing_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):161"""162Sleep an incremental amount of time after each attempt, starting at163wait_incrementing_start and incrementing by wait_incrementing_increment164"""165result = self._wait_incrementing_start + (self._wait_incrementing_increment * (previous_attempt_number - 1))166if result < 0:167result = 0168return result169170def exponential_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):171exp = 2 ** previous_attempt_number172result = self._wait_exponential_multiplier * exp173if result > self._wait_exponential_max:174result = self._wait_exponential_max175if result < 0:176result = 0177return result178179def never_reject(self, result):180return False181182def always_reject(self, result):183return True184185def should_reject(self, attempt):186reject = False187if attempt.has_exception:188reject |= self._retry_on_exception(attempt.value[1])189else:190reject |= self._retry_on_result(attempt.value)191192return reject193194def call(self, fn, *args, **kwargs):195start_time = int(round(time.time() * 1000))196attempt_number = 1197while True:198try:199attempt = Attempt(fn(*args, **kwargs), attempt_number, False)200except:201tb = sys.exc_info()202attempt = Attempt(tb, attempt_number, True)203204if not self.should_reject(attempt):205return attempt.get(self._wrap_exception)206207delay_since_first_attempt_ms = int(round(time.time() * 1000)) - start_time208if self.stop(attempt_number, delay_since_first_attempt_ms):209if not self._wrap_exception and attempt.has_exception:210# get() on an attempt with an exception should cause it to be raised, but raise just in case211raise attempt.get()212else:213raise RetryError(attempt)214else:215sleep = self.wait(attempt_number, delay_since_first_attempt_ms)216if self._wait_jitter_max:217jitter = random.random() * self._wait_jitter_max218sleep = sleep + max(0, jitter)219time.sleep(sleep / 1000.0)220221attempt_number += 1222223224class Attempt(object):225"""226An Attempt encapsulates a call to a target function that may end as a227normal return value from the function or an Exception depending on what228occurred during the execution.229"""230231def __init__(self, value, attempt_number, has_exception):232self.value = value233self.attempt_number = attempt_number234self.has_exception = has_exception235236def get(self, wrap_exception=False):237"""238Return the return value of this Attempt instance or raise an Exception.239If wrap_exception is true, this Attempt is wrapped inside of a240RetryError before being raised.241"""242if self.has_exception:243if wrap_exception:244raise RetryError(self)245else:246six.reraise(self.value[0], self.value[1], self.value[2])247else:248return self.value249250def __repr__(self):251if self.has_exception:252return "Attempts: {0}, Error:\n{1}".format(self.attempt_number, "".join(traceback.format_tb(self.value[2])))253else:254return "Attempts: {0}, Value: {1}".format(self.attempt_number, self.value)255256257class RetryError(Exception):258"""259A RetryError encapsulates the last Attempt instance right before giving up.260"""261262def __init__(self, last_attempt):263self.last_attempt = last_attempt264265def __str__(self):266return "RetryError[{0}]".format(self.last_attempt)267268269