This commit is contained in:
Timothy Jaeryang Baek
2026-03-17 17:58:01 -05:00
parent fcf7208352
commit de3317e26b
220 changed files with 17200 additions and 22836 deletions

View File

@@ -35,7 +35,7 @@ class RateLimiter:
self.enabled = enabled
def _bucket_key(self, key: str, bucket_index: int) -> str:
return f"{REDIS_KEY_PREFIX}:ratelimit:{key.lower()}:{bucket_index}"
return f'{REDIS_KEY_PREFIX}:ratelimit:{key.lower()}:{bucket_index}'
def _current_bucket(self) -> int:
return int(time.time()) // self.bucket_size
@@ -84,9 +84,7 @@ class RateLimiter:
self.r.expire(bucket_key, self.window + self.bucket_size)
# Collect buckets
buckets = [
self._bucket_key(key, now_bucket - i) for i in range(self.num_buckets + 1)
]
buckets = [self._bucket_key(key, now_bucket - i) for i in range(self.num_buckets + 1)]
counts = self.r.mget(buckets)
total = sum(int(c) for c in counts if c)
@@ -95,9 +93,7 @@ class RateLimiter:
def _get_count_redis(self, key: str) -> int:
now_bucket = self._current_bucket()
buckets = [
self._bucket_key(key, now_bucket - i) for i in range(self.num_buckets + 1)
]
buckets = [self._bucket_key(key, now_bucket - i) for i in range(self.num_buckets + 1)]
counts = self.r.mget(buckets)
return sum(int(c) for c in counts if c)