Skip to content

volo-rs/rate-limiter

Volo

This crate implements rate limiter layer for Volo-based service.

Different Implementations of Rate Limiter

This crate provides multiple implementation options, see:

  • [InaccurateBucketRateLimiter]
  • [ThreadingBucketRateLimiter]
  • [TokioBucketRateLimiter]

Quick Start

Here using [ThreadingBucketRateLimiter] for examples.

Volo gRPC Server

Add the required dependencies to the Cargo.toml file:

[dependencies]
# -- snip --
volo-concurrency-limiter = { version = "*", features = ["volo-grpc"] }
# -- snip --

Add the middleware layer to the server, which looks like this:

use std::net::SocketAddr;
use volo_example::S;
use volo_grpc::server::{Server, ServiceBuilder};
use volo_rate_limiter::{RateLimiterLayer, ThreadingBucketRateLimiter};

#[volo::main]
async fn main() {
    let addr: SocketAddr = "[::]:8080".parse().unwrap();
    let addr = volo::net::Address::from(addr);

    Server::new()
        .add_service(ServiceBuilder::new(volo_gen::volo::example::ItemServiceServer::new(S)).build())
        .layer_front(RateLimiterLayer(ThreadingBucketRateLimiter::new(
            std::time::Duration::from_secs(1), // for each 1 second time window
            100,                               // 100 requests is allowed
        )))
        .run(addr)
        .await
        .unwrap();
}

Volo Thrift Server

Add the required dependencies to the Cargo.toml file:

[dependencies]
# -- snip --
volo-concurrency-limiter = { version = "*", features = ["volo-thrift"] }
# -- snip --

Add the middleware layer to the server, which looks like this:

use std::net::SocketAddr;
use volo_example::S;
use volo_rate_limiter::{RateLimiterLayer, ThreadingBucketRateLimiter};

#[volo::main]
async fn main() {
    let addr: SocketAddr = "[::]:8080".parse().unwrap();
    let addr = volo::net::Address::from(addr);

    volo_gen::volo::example::ItemServiceServer::new(S)
        .layer_front(RateLimiterLayer(ThreadingBucketRateLimiter::new(
            std::time::Duration::from_secs(1),  // for each 1 second time window
            100,                                // 100 requests is allowed
        )))
        .run(addr)
        .await
        .unwrap();
}

License

Volo is dual-licensed under the MIT license and the Apache License (Version 2.0).

See LICENSE-MIT and LICENSE-APACHE for details.

Community

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages