mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-10 19:29:50 +08:00
Improve gateway transport and usage runtime
This commit is contained in:
@@ -438,6 +438,18 @@ impl RuntimeState {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rate_limit_count(&self, key: &str, bucket: u64) -> Result<u32, DataLayerError> {
|
||||
match self.backend.as_ref() {
|
||||
RuntimeStateBackend::Memory(memory) => memory.rate_limit_count(key, bucket),
|
||||
RuntimeStateBackend::Redis(redis) => Ok(redis
|
||||
.kv
|
||||
.get(key)
|
||||
.await?
|
||||
.and_then(|value| value.parse::<u32>().ok())
|
||||
.unwrap_or_default()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_add(&self, key: &str, member: &str) -> Result<bool, DataLayerError> {
|
||||
match self.backend.as_ref() {
|
||||
RuntimeStateBackend::Memory(memory) => Ok(memory.set_add(key, member).await),
|
||||
@@ -1445,6 +1457,13 @@ mod tests {
|
||||
.expect("first"),
|
||||
RateLimitCheck::Allowed { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
runtime
|
||||
.rate_limit_count(input.user_key, input.bucket)
|
||||
.await
|
||||
.expect("count after first"),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
runtime
|
||||
.check_and_consume_rate_limit(input)
|
||||
@@ -1455,6 +1474,13 @@ mod tests {
|
||||
limit: 1
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
runtime
|
||||
.rate_limit_count(input.user_key, input.bucket)
|
||||
.await
|
||||
.expect("count after reject"),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user