Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded the retry wrapper to wait between retries #19

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "snek-sploit"
version = "0.7.0"
version = "0.7.1"
description = "Python RPC client for Metasploit Framework"
authors = [
"Jiří Rája <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion snek_sploit/lib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _create_arguments(self, call_arguments: list, use_token: bool) -> list:

return arguments

@retry(tries=3, on_errors=(requests.RequestException,))
@retry(attempts=3, on_errors=(requests.RequestException,))
def call(
self, endpoint: str, arguments: list = None, use_token: bool = True, timeout: Union[float, tuple] = None
) -> RPCResponse:
Expand Down
14 changes: 9 additions & 5 deletions snek_sploit/util/retry.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
from functools import wraps, partial
import time


def retry(func=None, *, tries: int = 1, on_errors: tuple = None):
def retry(func=None, *, attempts: int = 1, on_errors: tuple = None, wait_on_error: bool = True):
"""
Retry a function if an error occurs.
:param func: Original function
:param tries: Number of times to retry
:param attempts: Number of times to retry
:param on_errors: On what errors to retry
:param wait_on_error: Whether to wait when an error occurs or not
:return: Wrapper
"""
if func is None:
return partial(retry, tries=tries, on_errors=on_errors)
return partial(retry, attempts=attempts, on_errors=on_errors, wait_on_error=wait_on_error)

if not on_errors:
on_errors = (Exception,)

@wraps(func)
def wrapper(*args, **kwargs):
for i in range(tries + 1):
for i in range(attempts):
try:
return func(*args, **kwargs)
except on_errors as ex:
if i == tries:
if i + 1 == attempts:
raise ex
if wait_on_error:
time.sleep(min(i + 1 * 3, 30))

return wrapper