-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(KVStore): resolve KVStore time window and expiration issues #41
base: main
Are you sure you want to change the base?
Conversation
- Fix increment/decrement methods to properly handle active time windows - Add KV_MIN_EXPIRATION_BUFFER to enforce Cloudflare's 60s minimum requirement - Extract updateRecord method to ensure consistent KV updates - Add calculateExpiration helper to handle expiration times correctly - Prevent totalHits from going below 0 in decrement method - Update docstrings with accurate behavior descriptions
@lumpinif is attempting to deploy a commit to the rhinobase Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks amazing!
I have left some small changes; once completed, I will merge this into the main. Thank you for this!
await this.namespace.delete(this.prefixKey(key)); | ||
} | ||
} | ||
import type { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you lint the code so that your changes only show up? You can run pnpm run format
, for biome
to format this code. This looks like there might be some issue with lineEnding
formatting in biome.json
Thanks:) Btw, I've noticed an error in the example at the end of the part where the nowSecond+60 is used: It should be showcasing the nowSecond+60 is used when the reset time is passed while the expiration is still not ended - this should not be affecting the implementation though, just an example error. Please ensure making sufficient tests on my bad codes! |
ref: improve the readability on calculateExpiration return
pr comment has been updated with correct showcase of where nowSeconds + 60 is used. |
Great! I will take a look 🙌🏼 |
Fixed KV Store Rate Limiter Implementation
Problems
The original implementation had two key issues with Cloudflare KV time handling:
Example of error ❌
It might work for the first request with ANY window > 60s, but it fails for late requests in ANY window size when:
Visual Timeline:
Solution
Enhanced the KVStore implementation to properly handle both rate limiting windows and KV expiration:
Separated Concerns
Window Management
The fix ensures:
Another case where nowSeconds + 60 is used:
Benefits
✅ Compliant with Cloudflare's 60-second minimum expiration requirement
✅ Accurate rate limiting for any window duration (even < 60s)
✅ Clean separation between rate limiting logic and KV storage requirements
✅ Improved code organization with helper methods
✅ Better type safety and error handling
Testing
Works correctly for all window durations: