-
Notifications
You must be signed in to change notification settings - Fork 14
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
Added requests retry mechanism for all requests call in _pulp_client #53
Added requests retry mechanism for all requests call in _pulp_client #53
Conversation
To make sure tool process isn't interrupted due network issues, retry mechanism based on urllib3 Retry was implemented. New configuration environment variables to customize this were added: - UBIPOP_HTTP_TOTAL_RETRIES - UBIPOP_HTTP_RETRY_BACKOFF
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.
Does this retry on Exceptions as well? Like ConnectionTimeout
ubipop/_pulp_client.py
Outdated
@@ -9,11 +13,27 @@ | |||
|
|||
_LOG = logging.getLogger("ubipop") | |||
|
|||
HTTP_TOTAL_RETRIES = os.environ.get("UBIPOP_HTTP_TOTAL_RETRIES", 10) | |||
UBIPOP_HTTP_RETRY_BACKOFF = os.environ.get("UBIPOP_HTTP_RETRY_BACKOFF", 1) |
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.
with backoff=1, it means that will retry after 0, 2, 4, 8 (secs) and so on, 10 times can take ages, maybe 0.1 or 0.2 would be a better number no?
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 took the value from existing project that we have for helping with requests to pulp. But for sure I'm willing to change to anything we agree on.
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.
maybe 5 times' enough
def __init__(self, *args, **kwargs): | ||
kwargs['max_retries'] = Retry( | ||
total=kwargs.get('total_retries', HTTP_TOTAL_RETRIES), | ||
status_forcelist=[500, 502, 503, 504], |
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.
some 4xx also make sense here, for example: 429 Too Many Requests
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.
Again, these are from existing projects. But sure we can add more status codes there
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.
Maybe makes sense to make that configurable.
- Added test params descriptions - Removed unneeded code - fixed linter errors
4dbac80
to
a66c02a
Compare
- fixed constant names inconsistency - fixed typecast for env variables
Sorry that I overlooked your comment, urllib3 also retries for errors like timeout, socket error, protocol error and similar. |
I had to rebased this, so please approve this again |
tests/test_pulp.py
Outdated
env_retries, retry_call, retry_args, ok_response, expected_retries): | ||
global HTTP_TOTAL_RETRIES | ||
if env_retries: | ||
HTTP_TOTAL_RETRIES = env_retries |
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.
Would be nice to save a restore original value.
tests/test_pulp.py
Outdated
HTTP_TOTAL_RETRIES = env_retries | ||
|
||
retries = [make_mock_response(err_status_code, 'Fake Http error') | ||
for _ in range(HTTP_TOTAL_RETRIES)[:-1]] + [make_mock_response(200, ok_response)] |
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.
range(HTTP_TOTAL_RETRIES - 1)
And the 'ok_response' could be added in the next line, would make the code more readable.
- backoff factor env variable type casted as float
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.
UBIPOP_HTTP_RETRY_BACKOFF should allow floats.
tests/test_pulp.py
Outdated
@@ -1,8 +1,30 @@ | |||
from ubipop._pulp_client import Repo, Package, ModuleDefaults, Pulp | |||
try: | |||
from importlib import reload |
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.
does this import make sense? not used, if fails nothing happens
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.
not sure what you mean. Python3 doesn't have reload globally available
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.
But where is used?
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.
Looks like reload is not used.
And if we need the import for an specific version better use sys.version_info.
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 see, I removed that code and forgot to remove that import
@@ -1,4 +1,7 @@ | |||
import requests | |||
from urllib3.util.retry import Retry | |||
import os |
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.
imports could be sorted better
) | ||
def test_retries(set_backoff_to_zero_fixture, mocked_getresponse, mock_pulp, should_retry, err_status_code, | ||
env_retries, retry_call, retry_args, ok_response, expected_retries): | ||
global HTTP_TOTAL_RETRIES |
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.
Just an idea, I don't like global keyword, the best way would be to use pulp_client.HTTP_TOTAL_RETRIES = value
.
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 think global is fine here, the variable is used in this scope.
tests/test_pulp.py
Outdated
HTTP_TOTAL_RETRIES = env_retries | ||
|
||
retries = [make_mock_response(err_status_code, 'Fake Http error') | ||
for _ in range(HTTP_TOTAL_RETRIES)[:-1]] |
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.
range(HTTP_TOTAL_RETRIES - 1)
@@ -9,11 +12,25 @@ | |||
|
|||
_LOG = logging.getLogger("ubipop") | |||
|
|||
HTTP_TOTAL_RETRIES = int(os.environ.get("UBIPOP_HTTP_TOTAL_RETRIES", 10)) | |||
HTTP_RETRY_BACKOFF = float(os.environ.get("UBIPOP_HTTP_RETRY_BACKOFF", 1)) |
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.
Personally, I think that the defined empty should be handled. But If nobody else thinks it's an issue I'm not requesting the change.
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.
Some minor issues, non-blocking. LGTM
- easier iteration over HTTP_TOTAL_RETRIES
To make sure tool process isn't interrupted due network issues,
retry mechanism based on urllib3 Retry was implemented.
New configuration environment variables to customize this
were added:
fixes #32