Skip to content
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

Add support for rate limiting #21

Merged
merged 6 commits into from Aug 24, 2018

Conversation

spyrbri
Copy link
Contributor

@spyrbri spyrbri commented Aug 21, 2018

Adding support for rate limiting.

Allow configuration of max_retries and backoff_factor.

@spyrbri spyrbri force-pushed the spyrbri/ch3381/add-support-for-rate-limiting branch from 9cd740e to e0651ae Compare August 21, 2018 14:07
@spyrbri spyrbri force-pushed the spyrbri/ch3381/add-support-for-rate-limiting branch from 235ac82 to 346c540 Compare August 22, 2018 08:19
bilbof
bilbof previously approved these changes Aug 22, 2018
README.md Outdated
* `max_retries=` sets the maximum number of retries for failed requests, default: 20
* `backoff_factor=` sets the exponential backoff factor, default: 2

Set it to 0 to disable it.
Copy link
Contributor

Choose a reason for hiding this comment

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

We need more detail in this sentence, should max_retries or backoff_factor be set to zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

def requests_retry_session(retries=20, backoff_factor=2, session=None,):
session = session or requests.Session()
adapter = _retry_adapter(retries, backoff_factor)
session.mount('http://', adapter)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this allow requests to be made over HTTP? If so, do you think we should remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

import unittest
import httpretty
import chartmogul
from unittest.mock import patch
Copy link
Contributor

Choose a reason for hiding this comment

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

This import doesn't seem used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was an obsolete test. Removed.

# backoff_factor set as 0 to avoid waiting while testing
config = Config("token", "secret", None, 4, 0)
try:
DataSource.create(config, data={ "test_date": date(2015, 1, 1) }).get()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a mock too right? Sorry I couldn't see where we mock it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is mocked by httpretty here

31         httpretty.register_uri(
32             httpretty.POST,
33             "https://api.chartmogul.com/v1/data_sources",
              ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you!

README.md Outdated
@@ -65,6 +65,17 @@ This throws error or returns `<Ping{data='pong!'}>`
You can also pass to the Config initializer:
* `request_timeout=` sets timeout for requests (seconds), default: none (see [requests docs](http://docs.python-requests.org/en/master/user/quickstart/#timeouts) for details)

### Rate Limits & Exponential Backoff
The library will keep retrying if the request exceeds the rate limit or if there's any network related error.
By default, the request will be retried for 20 times (approximated 15 minutes) before finally giving up.
Copy link
Contributor

Choose a reason for hiding this comment

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

'approximated' should be 'approximately'.

@bilbof bilbof dismissed their stale review August 22, 2018 08:54

Ah the build failed, dismissing my review.

@spyrbri spyrbri force-pushed the spyrbri/ch3381/add-support-for-rate-limiting branch from 210652d to d6b5795 Compare August 22, 2018 09:43
@spyrbri
Copy link
Contributor Author

spyrbri commented Aug 22, 2018

@bilbof Do you agree with the version changed to 1.2.0 instead of 1.1.9?

This looks like a backwards compatible feature more than a bug fix, right?

@bilbof
Copy link
Contributor

bilbof commented Aug 22, 2018

@spyrbri I think it's worth it. If a user has added their own retry logic for e.g. a 500 error, this may create bugs in their code - or at least, lots of additional unnecessary requests. Even if it doesn't, I don't see a negative from this small version bump.

I guess we'll need to create a release + release doc for this.

@pkopac pkopac merged commit 2f8d968 into master Aug 24, 2018
@pkopac pkopac deleted the spyrbri/ch3381/add-support-for-rate-limiting branch August 24, 2018 10:58
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.

None yet

3 participants