1 |
commit: 6d822cbc2a7a68e39583991b3f69ff76b032d585 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Mar 19 07:37:38 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Apr 2 16:53:23 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=6d822cbc |
7 |
|
8 |
Add ExponentialBackoff and RandomExponentialBackoff |
9 |
|
10 |
This will be useful as parameters for retry decorators. |
11 |
|
12 |
pym/portage/util/backoff.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ |
13 |
1 file changed, 53 insertions(+) |
14 |
|
15 |
diff --git a/pym/portage/util/backoff.py b/pym/portage/util/backoff.py |
16 |
new file mode 100644 |
17 |
index 000000000..ee39007ef |
18 |
--- /dev/null |
19 |
+++ b/pym/portage/util/backoff.py |
20 |
@@ -0,0 +1,53 @@ |
21 |
+# Copyright 2018 Gentoo Foundation |
22 |
+# Distributed under the terms of the GNU General Public License v2 |
23 |
+ |
24 |
+__all__ = ( |
25 |
+ 'ExponentialBackoff', |
26 |
+ 'RandomExponentialBackoff', |
27 |
+) |
28 |
+ |
29 |
+import random |
30 |
+import sys |
31 |
+ |
32 |
+ |
33 |
+class ExponentialBackoff(object): |
34 |
+ """ |
35 |
+ An object that when called with number of previous tries, calculates |
36 |
+ an exponential delay for the next try. |
37 |
+ """ |
38 |
+ def __init__(self, multiplier=1, base=2, limit=sys.maxsize): |
39 |
+ """ |
40 |
+ @param multiplier: constant multiplier |
41 |
+ @type multiplier: int or float |
42 |
+ @param base: maximum number of tries |
43 |
+ @type base: int or float |
44 |
+ @param limit: maximum number of seconds to delay |
45 |
+ @type limit: int or float |
46 |
+ """ |
47 |
+ self._multiplier = multiplier |
48 |
+ self._base = base |
49 |
+ self._limit = limit |
50 |
+ |
51 |
+ def __call__(self, tries): |
52 |
+ """ |
53 |
+ Given a number of previous tries, calculate the amount of time |
54 |
+ to delay the next try. |
55 |
+ |
56 |
+ @param tries: number of previous tries |
57 |
+ @type tries: int |
58 |
+ @return: amount of time to delay the next try |
59 |
+ @rtype: int |
60 |
+ """ |
61 |
+ try: |
62 |
+ return min(self._limit, self._multiplier * (self._base ** tries)) |
63 |
+ except OverflowError: |
64 |
+ return self._limit |
65 |
+ |
66 |
+ |
67 |
+class RandomExponentialBackoff(ExponentialBackoff): |
68 |
+ """ |
69 |
+ Equivalent to ExponentialBackoff, with an extra multiplier that uses |
70 |
+ a random distribution between 0 and 1. |
71 |
+ """ |
72 |
+ def __call__(self, tries): |
73 |
+ return random.random() * super(RandomExponentialBackoff, self).__call__(tries) |