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

Look into tower as a replacement for deadpool + channels #36

Open
mre opened this issue Nov 24, 2020 · 4 comments
Open

Look into tower as a replacement for deadpool + channels #36

mre opened this issue Nov 24, 2020 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@mre
Copy link
Member

mre commented Nov 24, 2020

While working on #33 I looked into existing libraries for network handling.
I even asked around on Twitter.

Turns out there is a neat little library called tower that was recommended by @shanesveller.
It handles many things that we currently do manually or not at all:

  • network timeouts
  • retries
  • batching

This part is particularly interesting:

This module defines a load-balanced pool of services that adds new services when load is high.

The pool uses poll_ready as a signal indicating whether additional services should be spawned
to handle the current level of load. Specifically, every time poll_ready on the inner service
returns Ready, [Pool] consider that a 0, and every time it returns Pending, [Pool]
considers it a 1. [Pool] then maintains an exponential moving
average
over those
samples, which gives an estimate of how often the underlying service has been ready when it was
needed "recently" (see [Builder::urgency]). If the service is loaded (see
[Builder::loaded_above]), a new service is created and added to the underlying [Balance].
If the service is underutilized (see [Builder::underutilized_below]) and there are two or
more services, then the latest added service is removed. In either case, the load estimate is
reset to its initial value (see [Builder::initial] to prevent services from being rapidly
added or removed.

(Sorry for quoting the entire thing, but I think it's quite rad.
AFAICT with that we can have some pool that optimizes network throughput.

If anyone has comments on the pros and cons of tower I'd love to hear them.
Perhaps somebody even wants to investigate and create a PR for it?

Help and feedback definitely wanted!

@mre mre added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Nov 24, 2020
@mre
Copy link
Member Author

mre commented Apr 6, 2021

Here is an example for how to wrap reqwest with tower: seanmonstar/reqwest#491

@mre
Copy link
Member Author

mre commented Mar 25, 2022

There's also https://github.com/antifuchs/governor. We could consider writing a middleware for tower around that.

@mre
Copy link
Member Author

mre commented Mar 25, 2022

Also drafted a rate limit parser yesterday: https://github.com/mre/rate-limit
There's a lot missing still (please create issues over there!), but I think we can use it to parse actual rate limits from the requests once we get there. That's much better than guessing the limits. Of course, we'd still have to have a sane default for sites that don't send rate limit headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant