Skip to content

Commit d389e75

Browse files
gautamg795Convex, Inc.
authored andcommitted
RateLimiter initial implementation (#25253)
GitOrigin-RevId: 852b26f0900216c159c0f463e6ded847cb6a28cc
1 parent b5d0c1c commit d389e75

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

Cargo.lock

Lines changed: 29 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/src/runtime/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use std::{
55
future::Future,
6+
hash::Hash,
67
num::TryFromIntError,
78
ops::{
89
Add,
@@ -27,6 +28,7 @@ pub use governor::nanos::Nanos;
2728
use governor::{
2829
middleware::NoOpMiddleware,
2930
state::{
31+
keyed::DefaultKeyedStateStore,
3032
InMemoryState,
3133
NotKeyed,
3234
},
@@ -312,10 +314,24 @@ pub type RateLimiter<RT> = governor::RateLimiter<
312314
NoOpMiddleware<<RuntimeClock<RT> as governor::clock::Clock>::Instant>,
313315
>;
314316

317+
pub type KeyedRateLimiter<K, RT> = governor::RateLimiter<
318+
K,
319+
DefaultKeyedStateStore<K>,
320+
RuntimeClock<RT>,
321+
NoOpMiddleware<<RuntimeClock<RT> as governor::clock::Clock>::Instant>,
322+
>;
323+
315324
pub fn new_rate_limiter<RT: Runtime>(runtime: RT, quota: Quota) -> RateLimiter<RT> {
316325
RateLimiter::direct_with_clock(quota, &RuntimeClock { runtime })
317326
}
318327

328+
pub fn new_keyed_rate_limiter<RT: Runtime, K: Hash + Eq + Clone>(
329+
runtime: RT,
330+
quota: Quota,
331+
) -> KeyedRateLimiter<K, RT> {
332+
KeyedRateLimiter::dashmap_with_clock(quota, &RuntimeClock { runtime })
333+
}
334+
319335
impl<RT: Runtime> governor::clock::Clock for RuntimeClock<RT> {
320336
type Instant = Nanos;
321337

crates/pb/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ pub mod storage {
4343
pub mod usage {
4444
include!(concat!(env!("OUT_DIR"), "/usage.rs"));
4545
}
46+
47+
pub const FILE_DESCRIPTOR_BYTES: &[u8] =
48+
include_bytes!(concat!(env!("OUT_DIR"), "/descriptors.bin"));

crates/pb_build/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::{
22
ffi::OsStr,
33
fs,
44
io::Result,
5-
path::Path,
5+
path::{
6+
Path,
7+
PathBuf,
8+
},
69
};
710

811
cfg_if::cfg_if! {
@@ -32,6 +35,7 @@ pub fn pb_build() -> Result<()> {
3235
println!("cargo:rerun-if-changed=protos");
3336
let mut paths = vec![];
3437
let mut packages = vec![];
38+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
3539
for dent in std::fs::read_dir("protos")? {
3640
let dent = dent?;
3741
let path = dent.path();
@@ -44,7 +48,9 @@ pub fn pb_build() -> Result<()> {
4448
));
4549
}
4650
}
47-
tonic_build::configure().compile(&paths, &["protos/"])?;
51+
tonic_build::configure()
52+
.file_descriptor_set_path(out_dir.join("descriptors.bin"))
53+
.compile(&paths, &["protos/"])?;
4854

4955
// We sort the package names just so we're generating the lib.rs
5056
// deterministically to avoid NOOP commits.
@@ -76,6 +82,11 @@ pub fn pb_build() -> Result<()> {
7682
));
7783
}
7884

85+
lib_file_contents.push_str(&format!(
86+
"\npub const FILE_DESCRIPTOR_BYTES: &[u8] =\n \
87+
include_bytes!(concat!(env!(\"OUT_DIR\"), \"/descriptors.bin\"));\n"
88+
));
89+
7990
let out_file = Path::new("src/lib.rs");
8091
if fs::read_to_string(out_file)? != lib_file_contents {
8192
fs::write(out_file, lib_file_contents)?;

0 commit comments

Comments
 (0)