-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from zmoon/retry
Retry
- Loading branch information
Showing
6 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from uscrn._util import retry | ||
|
||
|
||
def test_retry(caplog): | ||
from requests.exceptions import ConnectionError | ||
|
||
n = 0 | ||
|
||
@retry | ||
def func(): | ||
nonlocal n | ||
if n < 2: | ||
n += 1 | ||
raise ConnectionError | ||
return "result" | ||
|
||
with caplog.at_level("INFO"): | ||
res = func() | ||
|
||
for r in caplog.records: | ||
assert r.getMessage() == "Retrying func in 1 s after connection error" | ||
|
||
assert res == "result" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
|
||
logger = logging.getLogger("uscrn") | ||
|
||
|
||
def retry(func): | ||
"""Decorator to retry a function on web connection error. | ||
Up to 60 s, with Fibonacci backoff (1, 1, 2, 3, ...). | ||
""" | ||
import urllib | ||
from functools import wraps | ||
|
||
import requests | ||
|
||
max_time = 60_000_000_000 # 60 s (in ns) | ||
|
||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
from time import perf_counter_ns, sleep | ||
|
||
t0 = perf_counter_ns() | ||
a, b = 1, 1 | ||
while True: | ||
try: | ||
return func(*args, **kwargs) | ||
except ( | ||
urllib.error.URLError, | ||
requests.exceptions.ConnectionError, | ||
requests.exceptions.ReadTimeout, | ||
): | ||
if perf_counter_ns() - t0 > max_time: # pragma: no cover | ||
raise | ||
logger.info( | ||
f"Retrying {func.__name__} in {a} s after connection error", | ||
stacklevel=2, | ||
) | ||
sleep(a) | ||
a, b = b, a + b # Fibonacci backoff | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters