-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cruft PRs: wait for fork creation (#202)
- Loading branch information
1 parent
1929d97
commit e03a54d
Showing
3 changed files
with
37 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import random | ||
import time | ||
from collections.abc import Callable | ||
from typing import TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def retry_with_backoff( | ||
fn: Callable[[], T], | ||
retries: int = 5, | ||
backoff_in_seconds: int | float = 1, | ||
exc_cls: type = Exception, | ||
) -> T: | ||
exc = None | ||
for x in range(retries): | ||
try: | ||
return fn() | ||
except exc_cls as _exc: | ||
exc = _exc | ||
sleep = backoff_in_seconds * 2**x + random.uniform(0, 1) | ||
time.sleep(sleep) | ||
raise exc |
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