This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
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
6 changed files
with
192 additions
and
28 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,83 @@ | ||
# Copyright (c) 2013, SaltyCrane | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the | ||
# distribution. | ||
# | ||
# * Neither the name of the SaltyCrane nor the names of its | ||
# contributors may be used to endorse or promote products derived | ||
# from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import time | ||
from functools import wraps | ||
|
||
import logging | ||
|
||
from apiclient.errors import HttpError | ||
|
||
|
||
def google_http_error_retry(tries=4, delay=3, backoff=2, logger=logging): | ||
""" | ||
Retry calling the decorated function using an exponential backoff. | ||
It retries HttpErrors with status code > 400(except 404). | ||
Errors with another status code are reraised | ||
:param tries: number of times to try (not retry) before giving up | ||
:type tries: int | ||
:param delay: initial delay between retries in seconds | ||
:type delay: int | ||
:param backoff: backoff multiplier e.g. value of 2 will double the delay | ||
each retry | ||
:type backoff: int | ||
:param logger: logger to use. If None, print | ||
:type logger: logging.Logger instance | ||
""" | ||
def deco_retry(f): | ||
|
||
@wraps(f) | ||
def f_retry(*args, **kwargs): | ||
mtries, mdelay = tries, delay | ||
while mtries > 1: | ||
try: | ||
return f(*args, **kwargs) | ||
except HttpError as e: | ||
if e.resp.status > 400 and e.resp.status != 404: | ||
msg = "%s, Retrying '%s' because of %s args: %s " \ | ||
"in %d seconds..." \ | ||
% (str(e), f.__name__, type(e), e.args, mdelay) | ||
if logger: | ||
logger.warning(msg) | ||
else: | ||
print(msg) # pylint: disable=C0325 | ||
time.sleep(mdelay) | ||
mtries -= 1 | ||
mdelay *= backoff | ||
else: | ||
raise e | ||
|
||
return f(*args, **kwargs) | ||
|
||
return f_retry # true decorator | ||
|
||
return deco_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import unittest | ||
|
||
from apiclient.errors import HttpError | ||
|
||
from src.commons.decorators.google_http_error_retry import \ | ||
google_http_error_retry | ||
from mock import patch, mock | ||
|
||
|
||
class TestGoogleHttpErrorRetryDecorator(unittest.TestCase): | ||
|
||
class TestClass(object): | ||
def counter_for_400(self): | ||
pass | ||
|
||
def counter_for_404(self): | ||
pass | ||
|
||
def counter_for_500(self): | ||
pass | ||
|
||
@google_http_error_retry(tries=3, delay=0, backoff=1) | ||
def raise_HttpError400(self): | ||
self.counter_for_400() | ||
raise HttpError(mock.Mock(status=400), 'Bad request') | ||
|
||
@google_http_error_retry(tries=3, delay=0, backoff=1) | ||
def raise_HttpError404(self): | ||
self.counter_for_404() | ||
raise HttpError(mock.Mock(status=404), 'Not Found') | ||
|
||
@google_http_error_retry(tries=3, delay=0, backoff=1) | ||
def raise_HttpError500(self): | ||
self.counter_for_500() | ||
raise HttpError(mock.Mock(status=500), 'Internal error') | ||
|
||
@patch.object(TestClass, "counter_for_400") | ||
def test_that_http_400_errors_are_not_retried(self, counter_for_400): | ||
# when | ||
self.execute_function_and_suppress_exceptions( | ||
TestGoogleHttpErrorRetryDecorator.TestClass().raise_HttpError400) | ||
# then | ||
self.assertEquals(1, counter_for_400.call_count)\ | ||
|
||
|
||
@patch.object(TestClass, "counter_for_404") | ||
def test_that_http_404_errors_are_not_retried(self, counter_for_404): | ||
# when | ||
self.execute_function_and_suppress_exceptions( | ||
TestGoogleHttpErrorRetryDecorator.TestClass().raise_HttpError404) | ||
# then | ||
self.assertEquals(1, counter_for_404.call_count) | ||
|
||
@patch.object(TestClass, "counter_for_500") | ||
def test_that_http_500_errors_are_retried(self, counter_for_500): | ||
# when | ||
self.execute_function_and_suppress_exceptions( | ||
TestGoogleHttpErrorRetryDecorator.TestClass().raise_HttpError500) | ||
# then | ||
self.assertEquals(3, counter_for_500.call_count) | ||
|
||
@classmethod | ||
def execute_function_and_suppress_exceptions(cls, function): | ||
try: | ||
function() | ||
except Exception: | ||
pass |