Skip to content

Commit da6664d

Browse files
committed
add X-Ratelimit-Strategy header to responses
1 parent b045b2b commit da6664d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ To get an API token, log in with any oauth provider and enter the dashboard. Cop
3434
The way ratelimits work differs based on whether redis is being used or not.
3535
If redis is in use, the window method is used, where you have X requests available in a certain timeframe.
3636
If redis is NOT in use, the leaky bucket method is used, where you gain requests after every X seconds.
37-
The ratelimit headers will differ a slight bit depending on the method in use (ex x-ratelimit-reset is not applicable to the leaky bucket, and will always be 0)
37+
The ratelimit headers will differ a slight bit depending on the method in use (ex x-ratelimit-reset is not applicable to the leaky bucket, and will always be 0).
38+
You can always tell what method is being used via the `X-Ratelimit-Strategy` header. If this header returns `ignore`, ratelimits are not applicable to the request (ex if using an admin account).
3839

3940
All API responses have ratelimit information associated with them:
4041
- `x-ratelimit-available`: How many API calls you have left before being ratelimited.

mystbin/backend/utils/ratelimits.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class NoRedisConnection(Exception):
3232
}
3333

3434
class BaseLimitBucket:
35+
strategy: str
36+
3537
def __init__(self, app: MystbinApp, key: str, count: int, per: int) -> None:
3638
self.count = count
3739
self.per = per
@@ -57,6 +59,8 @@ class InMemoryLimitBucket(BaseLimitBucket):
5759

5860
__slots__ = ("count", "per", "hits")
5961

62+
strategy: str = "leakybucket"
63+
6064
def __init__(self, app: MystbinApp, key: str, count: int, per: int) -> None:
6165
self.count = count
6266
self.per = per
@@ -87,6 +91,8 @@ class RedisLimitBucket(BaseLimitBucket):
8791

8892
__slots__ = ("key", "count", "per", "app", "reset")
8993

94+
strategy: str = "window"
95+
9096
def __init__(self, app: MystbinApp, key: str, count: int, per: int) -> None:
9197
self.app = app
9298
self.key = key
@@ -179,7 +185,8 @@ async def middleware(self, request: Request, call_next: _CT) -> Response:
179185
"X-Ratelimit-Used": "0",
180186
"X-Ratelimit-Reset": "0",
181187
"X-Ratelimit-Max": "1",
182-
"X-Ratelimit-Available": "1"
188+
"X-Ratelimit-Available": "1",
189+
"X-Ratelimit-Strategy": self.bucket_cls.strategy
183190
}
184191

185192
if is_limited:
@@ -213,7 +220,8 @@ async def middleware(self, request: Request, call_next: _CT) -> Response:
213220
"X-Ratelimit-Used": "0",
214221
"X-Ratelimit-Reset": "0",
215222
"X-Ratelimit-Max": "1",
216-
"X-Ratelimit-Available": "1"
223+
"X-Ratelimit-Available": "1",
224+
"X-Ratelimit-Strategy": "ignore"
217225
}
218226
resp = await call_next(request)
219227
resp.headers.update(headers)

0 commit comments

Comments
 (0)