Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH 2/4] Add ExponentialBackoff and RandomExponentialBackoff
Date: Sun, 01 Apr 2018 02:52:02
Message-Id: 20180401024631.31017-3-zmedico@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH 0/4] rsync: add key refresh retry (bug 649276) by Zac Medico
1 This will be useful as parameters for retry decorators.
2 ---
3 pym/portage/util/backoff.py | 48 +++++++++++++++++++++++++++++++++++++++++++++
4 1 file changed, 48 insertions(+)
5 create mode 100644 pym/portage/util/backoff.py
6
7 diff --git a/pym/portage/util/backoff.py b/pym/portage/util/backoff.py
8 new file mode 100644
9 index 000000000..d8854337c
10 --- /dev/null
11 +++ b/pym/portage/util/backoff.py
12 @@ -0,0 +1,48 @@
13 +# Copyright 2018 Gentoo Foundation
14 +# Distributed under the terms of the GNU General Public License v2
15 +
16 +import random
17 +import sys
18 +
19 +
20 +class ExponentialBackoff(object):
21 + """
22 + An object that when called with number of previous tries, calculates
23 + an exponential delay for the next try.
24 + """
25 + def __init__(self, multiplier=1, base=2, limit=sys.maxsize):
26 + """
27 + @param multiplier: constant multiplier
28 + @type multiplier: int or float
29 + @param base: maximum number of tries
30 + @type base: int or float
31 + @param limit: maximum number of seconds to delay
32 + @type limit: int or float
33 + """
34 + self._multiplier = multiplier
35 + self._base = base
36 + self._limit = limit
37 +
38 + def __call__(self, tries):
39 + """
40 + Given a number of previous tries, calculate the amount of time
41 + to delay the next try.
42 +
43 + @param tries: number of previous tries
44 + @type tries: int
45 + @return: amount of time to delay the next try
46 + @rtype: int
47 + """
48 + try:
49 + return min(self._limit, self._multiplier * (self._base ** tries))
50 + except OverflowError:
51 + return self._limit
52 +
53 +
54 +class RandomExponentialBackoff(ExponentialBackoff):
55 + """
56 + Equivalent to ExponentialBackoff, with an extra multiplier that uses
57 + a random distribution between 0 and 1.
58 + """
59 + def __call__(self, tries):
60 + return random.random() * super(RandomExponentialBackoff, self).__call__(tries)
61 --
62 2.13.6