Skip to content

Conversation

@Matvey-Kuk
Copy link
Contributor

@Matvey-Kuk Matvey-Kuk commented Feb 23, 2018

In case of server error, library will try to repeat the same request 10 more times. 10 is by default. May be changed by user.

- 3.7-dev
- nightly
#- 3.7-dev
#- nightly
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaml library is not compiling with python3.7 yet :(

timeout = RETRY_TIMEOUT_IN_CASE_OF_SERVER_ERROR * (
self._server_error_retries - retries_left + 1)
logger.warning(
str(error.message) + " Retrying in " + str(timeout) +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like:

logger.warning(
    '%s Retrying in %d seconds. Retries left: %d',
    error.message, timeout, retries.left,
)

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

)
assert resp is True

def test_exceptions(self):
Copy link

@jcollado jcollado Feb 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that pytest is in the requirements, I'd recommend to reple all the
self.assert....

with:
assert ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it better?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest rewrites the code on the fly and replaces the assert statements with their own code.

This way, you just need to use assert and the comparison operator that you like instead of having to remember which is the best self.assertSomething function for a given comparison.

Aside from that, when the assertion fails, it will typically show you more information about the values that were passed rather than just the result of the expression. You can see more information about that here.

try:
# Wrap server error codes as exceptions
response = request_method(*args, **kwargs)
if round(response.status_code / 100) == 5:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that integer division would be more readable here. That is instead of:
round(response.status_code / 100) == 5

this:
(response.status_code // 100) == 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, note that round(451 / 100) is 5, not 4.

Copy link
Contributor Author

@Matvey-Kuk Matvey-Kuk Feb 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree about integer division here. By the way:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(451 / 100)
4.0
>>> round(459 / 100)
4.0
>>> round(499 / 100)
4.0
>>> 

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I checked with python 3. Anyway, from what I see, the code is expected to work with both python 2 and 3, so it's better not to use round just in case.

timeout = RETRY_TIMEOUT_IN_CASE_OF_SERVER_ERROR * (
self._server_error_retries - retries_left + 1)
logger.warning(
'%s Retrying in %d seconds. Retries left: %d' %

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it's preferable to let the logging module do the string interpolation for you, that is, replace:
logger.warning(<message> % <values)
with:
logger.warning(<message>, <values>)

The reason for this is that if log level is set to something above warning the interpolation won't happen. This might seem it doesn't make a difference, but in a big code base performance results might result of this, so in my opinion is a better option to do it always this way.

# Retry in case of server error
if retries_left <= 0:
raise error
timeout = RETRY_TIMEOUT_IN_CASE_OF_SERVER_ERROR * (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to increment the timemout linearly, while usually timeouts increment exponentially,
so I'd recommend something like:

timeout = <default>

try:
...
except <exception>:
     time.sleep(timeout)
     timeout **= 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you still prefer to increment the timeout linearly, a modified version of the code above is more readable in my opinion:

timeout = <default>

try:
...
except <exception>:
     time.sleep(timeout)
     timeout += <default>

@Matvey-Kuk Matvey-Kuk merged commit 53d68a3 into master Feb 28, 2018
@Matvey-Kuk Matvey-Kuk deleted the feature-more-500-tolerant branch February 28, 2018 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants