Improve gateway transport and usage runtime

This commit is contained in:
elky
2026-06-25 22:36:27 +08:00
parent d336d1a7fa
commit 6f00e9fc67
112 changed files with 12456 additions and 1387 deletions
+26
View File
@@ -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]