-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import time | ||
import functools | ||
|
||
|
||
def retry_on_exception(max_attempts=11, start_sleep_time=1, factor=2): | ||
def decorator_retry(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
attempts, sleep_time = 0, start_sleep_time | ||
while attempts < max_attempts: | ||
try: | ||
return func(*args, **kwargs) | ||
except ( | ||
Exception | ||
) as e: # TODO: Map only timeout errors or errors from many requests | ||
print( | ||
f"Retrying... Attempt {attempts + 1} after {sleep_time} seconds, {str(e)}" | ||
) | ||
time.sleep(sleep_time) | ||
attempts += 1 | ||
sleep_time *= factor | ||
|
||
print("Max retry attempts reached. Raising exception.") | ||
raise Exception("Rate limit exceeded, max retry attempts reached.") | ||
|
||
return wrapper | ||
|
||
return decorator_retry |
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
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