-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (44 loc) · 1.58 KB
/
Copy pathutils.py
File metadata and controls
51 lines (44 loc) · 1.58 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (c) The Diem Core Contributors
# SPDX-License-Identifier: Apache-2.0
import typing
import time
from functools import wraps
RT = typing.TypeVar("RT")
TFun = typing.Callable[..., typing.Optional[RT]]
def retry(
exceptions: typing.Type[Exception],
tries: int = 4,
delay: int = 3,
backoff: int = 2,
logger: typing.Optional[typing.Callable[[str], None]] = None,
) -> typing.Callable[[TFun], TFun]:
"""
Retry calling the decorated function using an exponential backoff.
Args:
exceptions: The exception to check. may be a tuple of
exceptions to check.
tries: Number of times to try (not retry) before giving up.
delay: Initial delay between retries in seconds.
backoff: Backoff multiplier (e.g. value of 2 will double the delay
each retry).
logger: Logger to use. If None, print.
"""
def deco_retry(f: TFun) -> TFun:
@wraps(f)
def f_retry(*args, **kwargs): # pyre-ignore
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except exceptions as e:
msg = "{}, Retrying in {} seconds...".format(e, mdelay)
if logger:
logger(msg)
else:
print(msg)
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return typing.cast(TFun, f_retry) # true decorator
return deco_retry