summaryrefslogtreecommitdiff
path: root/discord_rpc/util/backoff.py
blob: 650a8718201cebd80d2a1323984569cb06311b31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import absolute_import
import random
from .types import Int32, Int64


class Backoff(object):
    _min_amt = None
    _max_amt = None
    _current = None
    _fails = Int32()

    def __init__(self, min_amt, max_amt):
        min_amt = max(min_amt, 1)
        max_amt = max(max_amt, 1)
        self._min_amt = Int64(min_amt)
        self._max_amt = Int64(max_amt)
        self._current = Int64(min_amt)

    def reset(self):
        self._fails = Int32(0)
        self._current = self._min_amt.get_copy()

    def next_delay(self):
        self._fails += 1
        delay = Int64(self._current.get_number() * 2.0 * random.random())
        self._current = Int64(min(self._current.get_number() + delay.get_number(), self._max_amt))
        return self._current

    @property
    def fails(self):
        return self._fails

    @property
    def current(self):
        return self._current