-
Notifications
You must be signed in to change notification settings - Fork 10
feat(api): add connection retries and request timeout to API client #251
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
Open
ocervell
wants to merge
1
commit into
Syslifters:main
Choose a base branch
from
ocervell:feat/api-client-retries-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from urllib3.util.retry import Retry | ||
|
|
||
| import reptor.settings as settings | ||
| from reptor.lib.reptor import Reptor | ||
|
|
||
| from ..APIClient import APIClient | ||
|
|
||
|
|
||
| class TestAPIClientSession: | ||
| @pytest.fixture(autouse=True) | ||
| def setUp(self): | ||
| self.reptor = Reptor() | ||
| self.reptor._config._raw_config["server"] = "https://demo.sysre.pt" | ||
| self.reptor._config._raw_config["token"] = "sysreptor_test" | ||
| self.client = APIClient(reptor=self.reptor, require_project_id=False) | ||
|
|
||
| def test_build_session_mounts_retry_on_both_schemes(self): | ||
| for scheme in ("http://demo.sysre.pt", "https://demo.sysre.pt"): | ||
| adapter = self.client._session.get_adapter(scheme) | ||
| retry = adapter.max_retries | ||
| assert isinstance(retry, Retry) | ||
| assert retry.total == settings.API_MAX_RETRIES | ||
| assert retry.connect == settings.API_MAX_RETRIES | ||
| assert retry.read == settings.API_MAX_RETRIES | ||
| assert retry.status == settings.API_MAX_RETRIES | ||
| assert retry.backoff_factor == settings.API_RETRY_BACKOFF_FACTOR | ||
| assert list(retry.status_forcelist) == settings.API_RETRY_STATUS_FORCELIST | ||
| # Never turn an exhausted status-retry into an exception here; the | ||
| # existing response.raise_for_status() in _do_request owns that. | ||
| assert retry.raise_on_status is False | ||
| assert retry.respect_retry_after_header is True | ||
|
|
||
| def test_prepare_kwargs_sets_defaults(self): | ||
| prepared = self.client._prepare_kwargs({}) | ||
| assert prepared["timeout"] == settings.API_TIMEOUT | ||
| assert prepared["verify"] is True # insecure not set -> verify on | ||
| assert prepared["allow_redirects"] is False | ||
|
|
||
| def test_prepare_kwargs_respects_caller_overrides(self): | ||
| prepared = self.client._prepare_kwargs({"timeout": 5, "verify": False}) | ||
| assert prepared["timeout"] == 5 | ||
| assert prepared["verify"] is False | ||
|
|
||
| def test_insecure_config_disables_verify(self): | ||
| self.reptor._config._raw_config["insecure"] = True | ||
| client = APIClient(reptor=self.reptor, require_project_id=False) | ||
| assert client._prepare_kwargs({})["verify"] is False | ||
|
|
||
| def test_do_request_routes_through_session_with_defaults(self): | ||
| response = MagicMock() | ||
| response.headers = {} | ||
| response.content = b"{}" | ||
| self.client._session.get = MagicMock(return_value=response) | ||
|
|
||
| self.client.get("https://demo.sysre.pt/api/v1/pentestprojects/") | ||
|
|
||
| self.client._session.get.assert_called_once() | ||
| _, kwargs = self.client._session.get.call_args | ||
| assert kwargs["timeout"] == settings.API_TIMEOUT | ||
| assert kwargs["verify"] is True | ||
| assert kwargs["allow_redirects"] is False |
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 |
|---|---|---|
|
|
@@ -37,6 +37,12 @@ | |
| NEWLINE = "\n" | ||
|
|
||
| USER_AGENT = "reptor CLI v0.1.0" # TODO dynamic version | ||
|
|
||
| # HTTP client defaults (see reptor/api/APIClient.py) | ||
| API_TIMEOUT = 30 # per-request (connect, read) timeout in seconds; prevents indefinite hangs | ||
| API_MAX_RETRIES = 3 # retries for transient connection/TLS failures and retryable status codes | ||
| API_RETRY_BACKOFF_FACTOR = 0.5 # exponential backoff: 0s, 0.5s, 1s, 2s, ... | ||
| API_RETRY_STATUS_FORCELIST = [429, 500, 502, 503, 504] # only these statuses are retried | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The users should probably have the option to control those values (at least API_TIMEOUT) via CLI options of via their config. |
||
| LANGUAGE_CODE = "en" | ||
| FORMAT_MODULE_PATH = [] | ||
| LOCALE_PATHS = ( | ||
|
|
||
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.
A default timeout of 30 seconds prevents long-running requests to fail. This would affect, for example rendering processes of large reports or project exports. Maybe the timeout should be increased for those endpoints to max(5m, API_TIMEOUT)