You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore: Update rate limit to sliding window (10s) (#6730)
Updates the rate limit to use a sliding window
<!-- start pr-codex -->
---
## PR-Codex overview
This PR focuses on updating the rate limiting functionality in the `rateLimit` function to utilize a sliding window approach instead of a fixed window, enhancing how requests are counted over time.
### Detailed summary
- Renamed `RATE_LIMIT_WINDOW_SECONDS` to `SLIDING_WINDOW_SECONDS`.
- Modified Redis interaction to use `mget` for batch fetching request counts.
- Updated the logic to calculate total requests over the last 10 seconds.
- Removed `sampleRate` handling and its related logic.
- Changed error messages for exceeding rate limits.
- Adjusted tests to align with the new sliding window logic.
> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`
<!-- end pr-codex -->
errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
61
+
errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. Please upgrade your plan to get higher rate limits.`,
79
62
errorCode: "RATE_LIMIT_EXCEEDED",
80
63
};
81
64
}
82
65
83
-
// do not await this, it just needs to execute at all
84
-
(async()=>
85
-
// always incrementBy the amount specified for the key
86
-
awaitredis.incrby(key,increment).then(async()=>{
87
-
// if the initial request count was 0, set the key to expire in the future
88
-
if(requestCount===0){
89
-
awaitredis.expire(key,RATE_LIMIT_WINDOW_SECONDS);
66
+
// Non-blocking: increment the request count for the current second.
67
+
(async()=>{
68
+
try{
69
+
constkey=getRequestCountAtSecondCacheKey(
70
+
serviceScope,
71
+
team.id,
72
+
currentSecond,
73
+
);
74
+
awaitredis.incrby(key,increment);
75
+
// If this is the first time setting this key, expire it after the sliding window is past.
0 commit comments