Expand description
This crate provides Rust bindings for the Redis Cell module.
You can define a rate-limiting Policy in quite a few ways:
use redis_cell_rs::Policy;
use std::time::Duration;
const POLICY1: Policy = Policy::from_tokens_per_second(1);
const POLICY2: Policy = Policy::from_tokens_per_minute(100);
const POLICY3: Policy = Policy::from_tokens_per_hour(1_000);
const POLICY4: Policy = Policy::from_tokens_per_day(5_000);
const POLICY5: Policy = Policy::from_tokens_per_period(100, Duration::from_secs(100))
.max_burst(100)
.apply_tokens(2)
.name("general_policy");
const POLICY6: Policy = Policy::new(
/* burst */ 10,
/* tokens */ 100,
/* period */ Duration::from_secs(100),
/* apply */ 1
);A policy (accompanied by Key) can now be used to crate rate-limiting
request (Cmd), which - in its turn - can be turned into a Redis command
and sent over to the server using a Redis client. The response can then be
converted into Verdict.
use redis::{Cmd as RedisCmd, Client};
use redis_cell_rs::{Cmd, Key, Verdict, AllowedDetails, BlockedDetails};
let key = Key::pair("user123", "/api/infer");
let cmd: RedisCmd = Cmd::new(&key, &POLICY1).into();
let client = Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
let verdict: Verdict = cmd.query(&mut con).unwrap();
match verdict {
Verdict::Allowed(details) => {
let AllowedDetails {total, remaining, reset_after, .. } = details;
println!("total={}, remaining={}, reset_after={}", total, remaining, reset_after);
},
Verdict::Blocked(details) => {
let BlockedDetails {total, remaining, reset_after, retry_after, .. } = details;
println!(
"total={}, remaining={}, reset_after={}, retry_after={}",
total,
remaining,
reset_after,
retry_after,
);
}
}