Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/backoff.py
7795 views
# -*- coding: utf-8 -*-12"""3The MIT License (MIT)45Copyright (c) 2015-2020 Rapptz67Permission is hereby granted, free of charge, to any person obtaining a8copy of this software and associated documentation files (the "Software"),9to deal in the Software without restriction, including without limitation10the rights to use, copy, modify, merge, publish, distribute, sublicense,11and/or sell copies of the Software, and to permit persons to whom the12Software is furnished to do so, subject to the following conditions:1314The above copyright notice and this permission notice shall be included in15all copies or substantial portions of the Software.1617THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER23DEALINGS IN THE SOFTWARE.24"""2526import time27import random282930class ExponentialBackoff:31"""An implementation of the exponential backoff algorithm3233Provides a convenient interface to implement an exponential backoff34for reconnecting or retrying transmissions in a distributed network.3536Once instantiated, the delay method will return the next interval to37wait for when retrying a connection or transmission. The maximum38delay increases exponentially with each retry up to a maximum of392^10 * base, and is reset if no more attempts are needed in a period40of 2^11 * base seconds.4142Parameters43----------44base: :class:`int`45The base delay in seconds. The first retry-delay will be up to46this many seconds.47integral: :class:`bool`48Set to True if whole periods of base is desirable, otherwise any49number in between may be returned.50"""5152def __init__(self, base=1, *, integral=False):53self._base = base5455self._exp = 056self._max = 1057self._reset_time = base * 2**1158self._last_invocation = time.monotonic()5960# Use our own random instance to avoid messing with global one61rand = random.Random()62rand.seed()6364self._randfunc = rand.randrange if integral else rand.uniform6566def delay(self):67"""Compute the next delay6869Returns the next delay to wait according to the exponential70backoff algorithm. This is a value between 0 and base * 2^exp71where exponent starts off at 1 and is incremented at every72invocation of this method up to a maximum of 10.7374If a period of more than base * 2^11 has passed since the last75retry, the exponent is reset to 1.76"""77invocation = time.monotonic()78interval = invocation - self._last_invocation79self._last_invocation = invocation8081if interval > self._reset_time:82self._exp = 08384self._exp = min(self._exp + 1, self._max)85return self._randfunc(0, self._base * 2**self._exp)868788