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

add new param which allow to instant raise without retry for specified exception #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Rémy Greinhofer <[email protected]>
invlpg <[email protected]>
Richard O'Dwyer <[email protected]>
williara <[email protected]>
n0npax <[email protected]>
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ retry decorator
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param instant_raise_exceptions: an exception or tuple which will be raised without retry
"""

Various retrying logic can be achieved by combination of arguments.
Expand Down Expand Up @@ -124,6 +125,7 @@ retry_call
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param instant_raise_exceptions: an exception or tuple which will be raised without retry
:returns: the result of the f function.
"""

Expand Down
18 changes: 12 additions & 6 deletions retry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0,
logger=logging_logger):
logger=logging_logger, instant_raise_exceptions=None):
"""
Executes a function and retries it if it failed.

Expand All @@ -25,12 +25,15 @@ def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None,
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param instant_raise_exceptions: an exception or tuple which will be raised without retry
:returns: the result of the f function.
"""
_tries, _delay = tries, delay
while _tries:
try:
return f()
except instant_raise_exceptions:
raise
except exceptions as e:
_tries -= 1
if not _tries:
Expand All @@ -51,7 +54,8 @@ def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None,
_delay = min(_delay, max_delay)


def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger,
instant_raise_exceptions=None):
"""Returns a retry decorator.

:param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
Expand All @@ -63,6 +67,7 @@ def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, ji
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param instant_raise_exceptions: an exception or tuple which will be raised without retry
:returns: a retry decorator.
"""

Expand All @@ -71,14 +76,13 @@ def retry_decorator(f, *fargs, **fkwargs):
args = fargs if fargs else list()
kwargs = fkwargs if fkwargs else dict()
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
logger)
logger, instant_raise_exceptions)

return retry_decorator


def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
jitter=0,
logger=logging_logger):
jitter=0, logger=logging_logger, instant_raise_exceptions=None):
"""
Calls a function and re-executes it if it failed.

Expand All @@ -94,8 +98,10 @@ def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, dela
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param instant_raise_exceptions: an exception or tuple which will be raised without retry
:returns: the result of the f function.
"""
args = fargs if fargs else list()
kwargs = fkwargs if fkwargs else dict()
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger)
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger,
instant_raise_exceptions)
32 changes: 32 additions & 0 deletions tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ def f():
assert f() == target


def test_instant_raise_exceptions():

class MyException(Exception): pass
hit = [0]

@retry(instant_raise_exceptions=MyException)
def f():
hit[0] += 1
raise MyException

with pytest.raises(MyException):
f()
assert hit[0] == 1


def test_exceptions_and_instant_raise_exceptions():

class MyException(Exception): pass
hit = [0]

@retry(instant_raise_exceptions=MyException)
def f():
hit[0] += 1
if hit[0] > 5:
raise MyException
else:
raise IOError
with pytest.raises(MyException):
f()
assert hit[0] == 6


def test_max_delay(monkeypatch):
mock_sleep_time = [0]

Expand Down