Giter Site home page Giter Site logo

async-speed-limit's Introduction

async-speed-limit

Build status Latest Version Documentation

Asynchronously speed-limiting multiple byte streams (AsyncRead and AsyncWrite).

Usage

use async_speed_limit::Limiter;
use futures_util::{
    future::try_join,
    io::{self, AsyncRead, AsyncWrite},
};
use std::{marker::Unpin, pin::Pin};

async fn copy_both_slowly(
    r1: impl AsyncRead,
    w1: &mut (impl AsyncWrite + Unpin),
    r2: impl AsyncRead,
    w2: &mut (impl AsyncWrite + Unpin),
) -> io::Result<()> {
    // create a speed limiter of 1.0 KiB/s.
    let limiter = <Limiter>::new(1024.0);

    // limit both readers by the same queue
    // (so 1.0 KiB/s is the combined maximum speed)
    let r1 = limiter.clone().limit(r1);
    let r2 = limiter.limit(r2);

    // do the copy.
    try_join(io::copy(r1, w1), io::copy(r2, w2)).await?;
    Ok(())
}

Algorithm

async-speed-limit imposes the maximum speed limit using the token bucket algorithm with a single queue. Transmission is throttled by adding a delay proportional to the consumed tokens after it is sent. Because of this, the traffic flow will have high burstiness around when the token bucket is full.

The time needed to refill the bucket from empty to full is called the refill period. Reducing the refill period also reduces burstiness, but there would be more sleeps. The default value (0.1 s) should be good enough for most situations.

Tokio 0.1

async-speed-limit is designed for "Futures 0.3". This package does not directly support Tokio 0.1, but you can use futures-util's compat module to bridge the types.

Cargo features

Name Dependency Effect
standard-clock (default) futures-timer Enables the clock::StandardClock struct.
fused-future (default) futures-core Implements FusedFuture on limiter::Consume.
futures-io (default) futures-io Implements AsyncRead and AsyncWrite on limiter::Resource.
read-initializer futures-io Implements AsyncRead::initializer.
Unstable and requires nightly compiler to enable.

License

async-speed-limit is dual-licensed under

async-speed-limit's People

Contributors

kennytm avatar open-trade avatar overvenus avatar rustdesk avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

smartekit rxtxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.