Repository navigation
Retry requests #83
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
Merged
Merged
Retry requests #83
Changes from all 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
| import time | ||
| import requests | ||
|
|
||
| MAX_RETRIES_DEFAULT = 11 | ||
| BACKOFF_FACTOR = 1.0 | ||
|
|
||
|
|
||
| def _get_backoff_time(retry_count): | ||
| return BACKOFF_FACTOR * (2 ** retry_count) | ||
|
|
||
|
|
||
| class RetryRequests: | ||
| def __init__(self, max_requests_retries=MAX_RETRIES_DEFAULT) -> None: | ||
| self.max_retries = max_requests_retries | ||
|
|
||
| def _retry_request(self, request_func, url, *args, **kwargs): | ||
| response = request_func(url, *args, **kwargs) | ||
| for retry_count in range(self.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(self, url, *args, **kwargs): | ||
| return self._retry_request(requests.get, url, *args, **kwargs) | ||
|
|
||
| def post(self, url, *args, **kwargs): | ||
| return self._retry_request(requests.post, url, *args, **kwargs) | ||
|
|
||
| def put(self, url, *args, **kwargs): | ||
| return self._retry_request(requests.put, url, *args, **kwargs) | ||
|
|
||
| def delete(self, url, *args, **kwargs): | ||
| return self._retry_request(requests.delete, url, *args, **kwargs) | ||
This file contains hidden or 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,169 @@ | ||
| """ | ||
| Test that requests are retried if the server is not available. | ||
| """ | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import requests | ||
| import responses | ||
|
tomasfejfar marked this conversation as resolved.
Resolved
|
||
|
|
||
| from kbcstorage.base import Endpoint | ||
| from kbcstorage.tables import Tables | ||
|
|
||
| from .table_responses import list_response | ||
|
|
||
|
|
||
| class RetriesEndpoint(Endpoint): | ||
| def __init__(self, root_url, token, retries_count): | ||
| super().__init__(root_url, 'retries', token, max_requests_retries=retries_count) | ||
|
|
||
| def list(self): | ||
| return self._get(self.base_url) | ||
|
|
||
|
|
||
| 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) | ||
| self.no_retries = RetriesEndpoint(base_url, token, 0) | ||
| self.two_retries = RetriesEndpoint(base_url, token, 2) | ||
|
|
||
| @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() | ||
|
|
||
| @responses.activate | ||
| @patch('time.sleep', return_value=None) | ||
| def test_no_retries(self, sleep_mock): | ||
| """ | ||
| Request wont be retried for endpoints that configured it. | ||
| """ | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| status=502 | ||
| ) | ||
| ) | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| ) | ||
| ) | ||
| with self.assertRaises(requests.exceptions.HTTPError): | ||
| self.no_retries.list() | ||
|
|
||
| @responses.activate | ||
| @patch('time.sleep', return_value=None) | ||
| def test_two_retries_fail(self, sleep_mock): | ||
| """ | ||
| Request wont be retried for endpoints that configured it. | ||
| """ | ||
| for i in range(3): | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| status=502 | ||
| ) | ||
| ) | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| ) | ||
| ) | ||
| with self.assertRaises(requests.exceptions.HTTPError): | ||
| self.two_retries.list() | ||
|
|
||
| @responses.activate | ||
| @patch('time.sleep', return_value=None) | ||
| def test_two_retries_ok(self, sleep_mock): | ||
| """ | ||
| Request wont be retried for endpoints that configured it. | ||
| """ | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| status=502 | ||
| ) | ||
| ) | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/retries', | ||
| json=list_response, | ||
| ) | ||
| ) | ||
| assert isinstance(self.two_retries.list(), list) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.