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

INT-129: support rate-limit strategies #47

Merged
merged 2 commits into from
Jan 10, 2020
Merged

Conversation

rupa
Copy link
Contributor

@rupa rupa commented Dec 19, 2019

force pushed, see commit message

The existing rate limit handling is based on handling an exception when
we get a 429 response. We add some strategies that base behavior on the headers
from every response, without waiting for a 429. These are the same strategies
we have in the Go-SDK:

Adds two strategies:
  * solo - sleep longer the closer we are to running out of tokens
  * concurrent - needs a "parallelism" argument, accounts for the other workers
    in determining how long to sleep. doesn't kick in until tokens remaining
    is <= parallelism

Strategy is set and determined by config variables, the default (for now) is
"no strategy", i.e. preserves current behavior.

Added tests for resource: exercises transports and rate-limiting

There is an example of using rate-limiting strategies

I added support for these via config, and the different "transports". There is
some refactoring of transport internals to fix bugs, support various py
versions, and for ease of testing. Functionality should not be affected
in normal use:

* basic transport
  * pull ssl workaround for url openers into a method
  * replace references to `resp.hdrs` with a _get_headers function that
    should be consistent across py versions.
  * try/except around body.decode to handle str/bytes response from different
    py versions
* twisted transport
  * move ResourceException into class init. having it at top-level makes it
    tough to use e.g. py3.3 (without twisted).

Bonus:

* Update README about supported python versions and what we run CI against.
  turns out the "py33" env in tox.ini is actually 3.6
@rupa rupa changed the title WIP: INT-129: support rate-limit strategies INT-129: support rate-limit strategies Jan 7, 2020
@rupa
Copy link
Contributor Author

rupa commented Jan 7, 2020

I ended up amending each transport to handle rate limiting, originally I tried to do it all in the BaseResource, but twisted is the "odd one out" and it felt more wrong to handle all-but-one in the resource or thread headers up into higher-level code, than to just have a bit of handling in each transport.

Due to this, the logic for selecting/setting up the rate limit strategy ended up fitting best in the Config class, which kept the changes to transports fairly small.

Copy link
Contributor

@mburtless mburtless left a comment

Choose a reason for hiding this comment

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

🌭 I'm really liking this implementation and the general refactor of stuff like how opener gets set!

@rupa rupa merged commit ed406af into master Jan 10, 2020
@rupa rupa deleted the rate-limiting-strategies branch January 10, 2020 18:08
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.

2 participants