+
Skip to content

jimizai/ratelimit_rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ratelimit_rs

-- use ratelimit::Bucket;

The ratelimit package provides an efficient token bucket implementation. See http://en.wikipedia.org/wiki/Token_bucket.

Submit bugs See https://github.com/jimizai/ratelimit_rs/issues. I will deal with it in time.

Usage

struct Bucket

Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.

let bucket = Bucket::new(fill_interval: Duration, capacity: u64, quantum: u64, available_tokens: u64);

Bucket::new returns a new token bucket that fills at the rate of one token every fillInterval, up to the given maximum capacity. Both arguments must be positive. The bucket is initially full.

fn take_available

bucket.take_available(count: u64) -> u64;

TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.

fn take_max_duration

bucket.take_max_duration(count: u64, max_wait: Duration) -> (Duration, bool);

TakeMaxDuration is take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait.

If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.

fn wait_max_duration

bucket.wait_max_duration(count: u64, max_wait: Duration) -> bool;

WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.

About

ratelimit rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载