Skip to content

Bandwidth Rate limiting #2398

Description

@staticgc

Use-Case: Large buffers (~ 1MB) and large number of them (~millions) are to be transferred by the client and exploring hyper for it. Need a way to limit the network bandwidth used.

I see a similar ticket 2163 which was closed and hence opening a new one.

Is there a way to control n/w bandwidth in hyper?

Activity

  1. aeryz commented on Jan 13, 2021

    @aeryz
    Contributor

    As @seanmonstar said (#2163 (comment)), hyper uses streamed bodies so you should be able to control the rate of reads and writes.

  2. staticgc commented on Jan 13, 2021

    @staticgc
    Author

    How on earth did I miss that comment? It seems I read it but did not process it.
    Thanks @aeryz

  3. staticgc commented on Jan 29, 2021

    @staticgc
    Author

    hyper uses streamed bodies

    I explored it and I am still confused.

    My understanding is that hyper Client has a pool of tcp connections. So at a client level (i.e. hyper::client::Client) if I have to control the bytes written, how can I do that?

    This implies that consumer of the client will specify say "do not use more than 20 MB/s". This needs to be split across underlying connections, which is further complicated by the fact that number of connections is dynamic.

  4. changed the title [-]Bandwith Rate limiting[/-] [+]Bandwidth Rate limiting[/+] on Jan 29, 2021
  5. staticgc commented on Jan 30, 2021

    @staticgc
    Author

    How about this?

    I have used the limiter in the crate: async-speed-limit

    Now we:

    • Create a new type MyStream which wraps tokio::net::TcpStream and implements AsyncWrite & AsyncRead
    • Implement a connector MyConnector similar to HttpConnector which helps to create new MyStream
    • Instantiate Limiter and attach to MyConnector
    • Make MyConnector to attach clone of Limiter to every new MyStream (done via fn call() in MyConnector)

    The Limiter is thread-safe & cheaply clone-able. They all share the same bucket wrapped in Arc.

    The MyStream's fn poll_write calls the limiter to "consume" and wait if not enough tokens.

    Reference gist: here

    Usage

        use hyper::{self, Body, body::HttpBody};
    
    
        let builder = hyper::client::Client::builder();
        let connector = MyConnector::with_limit(102400.0); // ~ 100KB/s Limit
        let client = Arc::new(builder.build::<MyConnector, Body>(connector));

    Does it work?

    Initial results does seem to suggest that it is working

  6. aeryz commented on Feb 3, 2021

    @aeryz
    Contributor

    Hey, although I haven't tried it yet, your solution looks good to me. Since all connections use the same limiter, it would be able to limit the overall bandwidth rate perfectly.

  7. locked and limited conversation to collaborators on Feb 3, 2021
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions