-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(whitelist): add whitelist and blacklist support
- Loading branch information
Showing
14 changed files
with
255 additions
and
33 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,56 @@ | ||
# -*- coding: utf-8 -*- | ||
import riprova | ||
|
||
|
||
# Custom error object | ||
class MyCustomError(Exception): | ||
pass | ||
|
||
|
||
# Blacklist of errors that should exclusively be retried | ||
blacklist = riprova.ErrorBlacklist([ | ||
RuntimeError, | ||
MyCustomError | ||
]) | ||
|
||
|
||
def error_evaluator(error): | ||
""" | ||
Used to determine if an error is legit and therefore | ||
should be retried or not. | ||
""" | ||
return blacklist.isretry(error) | ||
|
||
|
||
# In order to define a global blacklist policy that would be used | ||
# across all retry instances, overwrite the blacklist attribute in Retrier. | ||
# NOTE: blacklist overwrites any whitelist. They are mutually exclusive. | ||
riprova.Retrier.blacklist = blacklist | ||
|
||
# Store number of function calls for error simulation | ||
calls = 0 | ||
|
||
|
||
# Register retriable operation with a custom error evaluator | ||
# You should pass the evaluator per retry instance. | ||
@riprova.retry(error_evaluator=error_evaluator) | ||
def mul2(x): | ||
global calls | ||
|
||
if calls < 3: | ||
calls += 1 | ||
raise RuntimeError('simulated call error') | ||
|
||
if calls == 3: | ||
calls += 1 | ||
raise Exception('non blacklisted error') | ||
|
||
return x * 2 | ||
|
||
|
||
# Run task | ||
try: | ||
mul2(2) | ||
except Exception as err: | ||
print('Blacklisted error: {}'.format(err)) | ||
print('Retry attempts: {}'.format(calls)) |
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ def mul2(x): | |
|
||
# Run task | ||
result = mul2(2) | ||
print('Result:', result) | ||
print('Result: {}'.format(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 |
---|---|---|
|
@@ -19,4 +19,4 @@ def mul2(x): | |
|
||
# Run task | ||
result = mul2(2) | ||
print('Result:', result) | ||
print('Result: {}'.format(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 |
---|---|---|
|
@@ -22,4 +22,4 @@ def mul2(x): | |
|
||
# Run task | ||
result = mul2(2) | ||
print('Result:', result) | ||
print('Result: {}'.format(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 |
---|---|---|
|
@@ -22,4 +22,4 @@ def mul2(x): | |
|
||
# Run task | ||
result = mul2(2) | ||
print('Result:', result) | ||
print('Result: {}'.format(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
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,58 @@ | ||
# -*- coding: utf-8 -*- | ||
import riprova | ||
|
||
|
||
# Custom error object | ||
class MyCustomError(Exception): | ||
pass | ||
|
||
|
||
# Whitelist of errors that should not be retried | ||
whitelist = riprova.ErrorWhitelist([ | ||
ReferenceError, | ||
ImportError, | ||
IOError, | ||
SyntaxError, | ||
IndexError | ||
]) | ||
|
||
|
||
def error_evaluator(error): | ||
""" | ||
Used to determine if an error is legit and therefore | ||
should be retried or not. | ||
""" | ||
return whitelist.isretry(error) | ||
|
||
|
||
# In order to define a global whitelist policy that would be used | ||
# across all retry instances, overwrite the whitelist attribute in Retrier: | ||
riprova.Retrier.whitelist = whitelist | ||
|
||
# Store number of function calls for error simulation | ||
calls = 0 | ||
|
||
|
||
# Register retriable operation with a custom error evaluator | ||
# You should pass the evaluator per retry instance. | ||
@riprova.retry(error_evaluator=error_evaluator) | ||
def mul2(x): | ||
global calls | ||
|
||
if calls < 3: | ||
calls += 1 | ||
raise RuntimeError('simulated call error') | ||
|
||
if calls == 3: | ||
calls += 1 | ||
raise ReferenceError('legit error') | ||
|
||
return x * 2 | ||
|
||
|
||
# Run task | ||
try: | ||
mul2(2) | ||
except ReferenceError as err: | ||
print('Whitelisted error: {}'.format(err)) | ||
print('Retry attempts: {}'.format(calls)) |
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
Oops, something went wrong.