Repository navigation
Bandwidth Rate limiting #2398
Description
Activity
As @seanmonstar said (#2163 (comment)), hyper uses streamed bodies so you should be able to control the rate of reads and writes.
How on earth did I miss that comment? It seems I read it but did not process it.
Thanks @aeryzhyper 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.
How about this?
I have used the limiter in the crate: async-speed-limit
Now we:
- Create a new type
MyStreamwhich wrapstokio::net::TcpStreamand implementsAsyncWrite&AsyncRead - Implement a connector
MyConnectorsimilar toHttpConnectorwhich helps to create newMyStream - Instantiate Limiter and attach to
MyConnector - Make
MyConnectorto attach clone ofLimiterto every newMyStream(done viafn call()inMyConnector)
The
Limiteris thread-safe & cheaply clone-able. They all share the same bucket wrapped in Arc.The
MyStream'sfn poll_writecalls 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
- Create a new type
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.
- locked and limited conversation to collaborators
on Feb 3, 2021
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?