-
Notifications
You must be signed in to change notification settings - Fork 7
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
Retry requests #83
Merged
Merged
Retry requests #83
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5b597be
Retry requests
Kukant 2fa6f42
adjust logic
Kukant 2d3e716
adjust max retries
Kukant ef0452b
docker-compose -> docker compose
Kukant 7da4081
fix whitespace
Kukant 46eed86
fix imports
Kukant dc1369a
asd
Kukant 207ffc4
make max number of retries a param
Kukant 10003c3
Test also changing the max retry attemps.
Kukant df86260
improve tests
Kukant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import time | ||
import requests | ||
|
||
MAX_RETRIES = 11 | ||
BACKOFF_FACTOR = 1.0 | ||
|
||
def _get_backoff_time(retry_count): | ||
tomasfejfar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return BACKOFF_FACTOR * (2 ** retry_count) | ||
|
||
def _retry_request(request_func, url, *args, **kwargs): | ||
response = request_func(url, *args, **kwargs) | ||
for retry_count in range(MAX_RETRIES - 1): | ||
if response.status_code == 501 or response.status_code < 500: | ||
return response | ||
time.sleep(_get_backoff_time(retry_count)) | ||
response = request_func(url, **kwargs) | ||
return response | ||
|
||
def get(url, *args, **kwargs): | ||
return _retry_request(requests.get, url, *args, **kwargs) | ||
|
||
def post(url, *args, **kwargs): | ||
return _retry_request(requests.post, url, *args, **kwargs) | ||
|
||
def put(url, *args, **kwargs): | ||
return _retry_request(requests.put, url, *args, **kwargs) | ||
|
||
def delete(url, *args, **kwargs): | ||
return _retry_request(requests.delete, url, *args, **kwargs) |
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,88 @@ | ||
""" | ||
Test basic functionality of the Tables endpoint | ||
""" | ||
import time | ||
import unittest | ||
from unittest.mock import patch | ||
from urllib.error import HTTPError | ||
|
||
import requests | ||
import responses | ||
tomasfejfar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from kbcstorage.tables import Tables | ||
|
||
from .table_responses import list_response | ||
|
||
|
||
class TestRequestRetry(unittest.TestCase): | ||
""" | ||
Test that requests are retried. | ||
""" | ||
def setUp(self): | ||
token = 'dummy_token' | ||
base_url = 'https://connection.keboola.com/' | ||
self.tables = Tables(base_url, token) | ||
|
||
@responses.activate | ||
@patch('time.sleep', return_value=None) | ||
def test_ok(self, sleep_mock): | ||
""" | ||
Retry will try at least 5 times. | ||
""" | ||
for _ in range(4): | ||
responses.add( | ||
responses.Response( | ||
method='GET', | ||
url='https://connection.keboola.com/v2/storage/tables', | ||
json=list_response, | ||
status=502 | ||
) | ||
) | ||
responses.add( | ||
responses.Response( | ||
method='GET', | ||
url='https://connection.keboola.com/v2/storage/tables', | ||
json=list_response, | ||
) | ||
) | ||
tables_list = self.tables.list() | ||
assert isinstance(tables_list, list) | ||
|
||
@responses.activate | ||
@patch('time.sleep', return_value=None) | ||
def test_raises_error_many_tries(self, sleep_mock): | ||
""" | ||
Retry will fail if it gets enough of error responses. | ||
""" | ||
for _ in range(20): | ||
responses.add( | ||
responses.Response( | ||
method='GET', | ||
url='https://connection.keboola.com/v2/storage/tables', | ||
json=list_response, | ||
status=502 | ||
) | ||
) | ||
responses.add( | ||
responses.Response( | ||
method='GET', | ||
url='https://connection.keboola.com/v2/storage/tables', | ||
json=list_response, | ||
) | ||
) | ||
with self.assertRaises(requests.exceptions.HTTPError): | ||
self.tables.list() | ||
|
||
@responses.activate | ||
@patch('time.sleep', return_value=None) | ||
def test_raises_error_on_4xx(self, sleep_mock): | ||
responses.add( | ||
responses.Response( | ||
method='GET', | ||
url='https://connection.keboola.com/v2/storage/tables', | ||
json=list_response, | ||
status=401 | ||
) | ||
) | ||
with self.assertRaises(requests.exceptions.HTTPError): | ||
self.tables.list() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 10 retries? That means 17 minutes request if the service is down completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://backoffcalculator.com/?interval=1&attempts=10&rate=2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have 10 retries in SAPI, do we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/keboola/storage-api-php-client/blob/master/src/Keboola/StorageApi/Client.php#L82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tak jsme to rozmotali, jak to je. Zásadní totiž je, že v php clientovi je to konfigurovatelné. Tím pádem tam, kde je to nějaký worker, který běží nekonečně je použitý default nebo i víc. A pak když je to používané někde v rámci requestu, tak je to zkrácené třeba na 3 pokusy. Tak prosím tady stejně. Tj. konfigurovatelné parametrem konstruktoru jako v té PHP verzi.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hotovson