Skip to content

Bandwidth Rate limiting #2416

Answered by staticgc
staticgc asked this question in Q&A
Discussion options

You must be logged in to vote

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 build…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by seanmonstar
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2398 on February 03, 2021 02:14.