Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/backoff.py
7795 views
1
# -*- coding: utf-8 -*-
2
3
"""
4
The MIT License (MIT)
5
6
Copyright (c) 2015-2020 Rapptz
7
8
Permission is hereby granted, free of charge, to any person obtaining a
9
copy of this software and associated documentation files (the "Software"),
10
to deal in the Software without restriction, including without limitation
11
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
and/or sell copies of the Software, and to permit persons to whom the
13
Software is furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
DEALINGS IN THE SOFTWARE.
25
"""
26
27
import time
28
import random
29
30
31
class ExponentialBackoff:
32
"""An implementation of the exponential backoff algorithm
33
34
Provides a convenient interface to implement an exponential backoff
35
for reconnecting or retrying transmissions in a distributed network.
36
37
Once instantiated, the delay method will return the next interval to
38
wait for when retrying a connection or transmission. The maximum
39
delay increases exponentially with each retry up to a maximum of
40
2^10 * base, and is reset if no more attempts are needed in a period
41
of 2^11 * base seconds.
42
43
Parameters
44
----------
45
base: :class:`int`
46
The base delay in seconds. The first retry-delay will be up to
47
this many seconds.
48
integral: :class:`bool`
49
Set to True if whole periods of base is desirable, otherwise any
50
number in between may be returned.
51
"""
52
53
def __init__(self, base=1, *, integral=False):
54
self._base = base
55
56
self._exp = 0
57
self._max = 10
58
self._reset_time = base * 2**11
59
self._last_invocation = time.monotonic()
60
61
# Use our own random instance to avoid messing with global one
62
rand = random.Random()
63
rand.seed()
64
65
self._randfunc = rand.randrange if integral else rand.uniform
66
67
def delay(self):
68
"""Compute the next delay
69
70
Returns the next delay to wait according to the exponential
71
backoff algorithm. This is a value between 0 and base * 2^exp
72
where exponent starts off at 1 and is incremented at every
73
invocation of this method up to a maximum of 10.
74
75
If a period of more than base * 2^11 has passed since the last
76
retry, the exponent is reset to 1.
77
"""
78
invocation = time.monotonic()
79
interval = invocation - self._last_invocation
80
self._last_invocation = invocation
81
82
if interval > self._reset_time:
83
self._exp = 0
84
85
self._exp = min(self._exp + 1, self._max)
86
return self._randfunc(0, self._base * 2**self._exp)
87
88