Browse Source

Type-hint backoff.py

pull/7190/head
Josh 4 years ago
committed by GitHub
parent
commit
e0a9365d61
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      discord/backoff.py

36
discord/backoff.py

@ -22,14 +22,20 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
from __future__ import annotations
import time import time
import random import random
from typing import Callable, Generic, Literal, TypeVar, overload, Union
T = TypeVar('T', bool, Literal[True], Literal[False])
__all__ = ( __all__ = (
'ExponentialBackoff', 'ExponentialBackoff',
) )
class ExponentialBackoff: class ExponentialBackoff(Generic[T]):
"""An implementation of the exponential backoff algorithm """An implementation of the exponential backoff algorithm
Provides a convenient interface to implement an exponential backoff Provides a convenient interface to implement an exponential backoff
@ -51,21 +57,33 @@ class ExponentialBackoff:
number in between may be returned. number in between may be returned.
""" """
def __init__(self, base=1, *, integral=False): def __init__(self, base: int = 1, *, integral: T = False):
self._base = base self._base: int = base
self._exp = 0 self._exp: int = 0
self._max = 10 self._max: int = 10
self._reset_time = base * 2 ** 11 self._reset_time: int = base * 2 ** 11
self._last_invocation = time.monotonic() self._last_invocation: float = time.monotonic()
# Use our own random instance to avoid messing with global one # Use our own random instance to avoid messing with global one
rand = random.Random() rand = random.Random()
rand.seed() rand.seed()
self._randfunc = rand.randrange if integral else rand.uniform self._randfunc: Callable[..., Union[int, float]] = rand.randrange if integral else rand.uniform # type: ignore
@overload
def delay(self: ExponentialBackoff[Literal[False]]) -> float:
...
@overload
def delay(self: ExponentialBackoff[Literal[True]]) -> int:
...
@overload
def delay(self: ExponentialBackoff[bool]) -> Union[int, float]:
...
def delay(self): def delay(self) -> Union[int, float]:
"""Compute the next delay """Compute the next delay
Returns the next delay to wait according to the exponential Returns the next delay to wait according to the exponential

Loading…
Cancel
Save