#rate-limiting #qps

ratelimiter-rs

为 Rust 定制的轻量级限速工具

6 个版本

0.1.5 2023年12月17日
0.1.4 2023年12月17日
0.1.3 2023年4月17日

#ratelimit 中排名第 1

Download history 72/week @ 2024-04-01 24/week @ 2024-04-08 22/week @ 2024-04-15 17/week @ 2024-04-29 37/week @ 2024-05-06 12/week @ 2024-05-13 54/week @ 2024-05-20 54/week @ 2024-05-27 15/week @ 2024-06-03 40/week @ 2024-06-10 24/week @ 2024-06-17 14/week @ 2024-06-24 10/week @ 2024-07-01 25/week @ 2024-07-08 13/week @ 2024-07-15

每月下载 72
被用于 rnacos

MIT/Apache 许可

11KB
277 行代码(不含注释)

ratelimiter-rs

为 Rust 定制的轻量级限速工具。

示例

use std::thread;
use std::time::Duration;
use ratelimiter_rs::{QpsLimiter,RateLimiter,AtomicRateLimiter,now_millis};

fn qpslimiter(){
    let mut limiter = QpsLimiter::new(10);
    // AtomicRateLimiter can clone to other thread and use
    //let limiter = AtomicRateLimiter::new(10);
    let mut times = 0;
    for _ in 0..3000 {
        thread::sleep(Duration::from_millis(1));
        if limiter.acquire() {
            times +=1;
            //println!("time: {}",now_millis())
        }
        else{
            continue;
        }
    }
    println!("time: {}, times: {}",now_millis(),&times);
}

无运行时依赖