Skip to content

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

Closed

Description

@mre

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!

Activity

  1. added
    enhancement
    good first issue
    help wanted
    on Nov 24, 2020
  2. mre commented on Apr 6, 2021

    @mre
    MemberAuthor

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

  3. mre commented on Mar 25, 2022

    @mre
    MemberAuthor

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

  4. mre commented on Mar 25, 2022

    @mre
    MemberAuthor

    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.

  5. mre commented on Jan 6, 2025

    @mre
    MemberAuthor

    lychee's architecture has diverged significantly from tower's model. It's unlikely at this point that we will adopt it for lychee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issuehelp wanted

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions