-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat: multiple rate limits and duration parameter (#55) #79
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
base: master
Are you sure you want to change the base?
Conversation
4ce49f3 to
f81b937
Compare
- Add optional limits array: each entry { maxRequests, duration }.
A request is sent only when every window allows one more; each window
resets independently.
- Add duration parameter: string with unit ms, s, m, h (e.g. '1s', '2m').
Invalid duration throws with message hinting at valid formats.
- Backward compatible: single limit via maxRequests + perMilliseconds or
maxRPS unchanged; getMaxRPS/setMaxRPS/setRateLimitOptions work as before.
- Update typings: RateLimitEntry, limits?, duration?.
- Add Unreleased section to CHANGELOG with format and compat notes.
f81b937 to
b1879c4
Compare
- Update parseDuration to throw an error for negative duration values. - Adjust test case to check for invalid duration handling with a negative value.
- Introduced throwDurationError function to centralize error messaging for unrecognized durations. - Updated parseDuration to utilize this new function for improved readability and maintainability.
- Implemented a new test case to verify that setRateLimitOptions correctly processes queued requests when the queue is non-empty. - The test checks the behavior of the rate limiter with multiple limits and ensures that queued requests are handled as expected after updating rate limit options.
- Introduced a new test case to verify that setRateLimitOptions maintains the integrity of the request queue when provided with an invalid duration format. - The test ensures that the rate limiter correctly throws an error for invalid duration inputs while allowing queued requests to be processed as expected.
- Created a new documentation file outlining testing instructions and guidelines for fixing issues. - Included steps for running tests and linter, as well as recommendations for creating regression tests and ensuring code quality.
- Revised the rate limit setup example to utilize an array of limits with specified durations. - Updated the usage examples for `http.get` and `http.getQueue` to align with the new configuration. - Enhanced documentation on setting rate limit options, including multiple limits and duration parameters.
- Added validation checks in buildWindows to ensure maxRequests and perMs are positive numbers. - Introduced error messages for invalid rate limit configurations, including checks for limits with missing or invalid maxRequests. - Expanded test coverage to verify that various invalid configurations throw appropriate errors when setting rate limit options.
- Modified the duration property in RateLimitEntry and rateLimitOptions to accept both string and number types, enhancing flexibility in rate limit configurations.
- Updated the parseDuration function to remove the radix parameter from parseFloat calls, improving compatibility with various input formats.
- Implemented checks in the buildWindows function to ensure the duration is a positive finite number, throwing an error for invalid values. - Added test cases to verify that invalid duration inputs (0 and Infinity) correctly trigger error messages.
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.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| @@ -1,6 +1,100 @@ | |||
| var DURATION_MSG = " Expected format: number+unit ms, s, m, h (e.g. '1s')." | |||
|
|
|||
| var DURATION_UNITS = { ms: 1, s: 1000, m: 60000, h: 3600000 } | |||
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.
Unused ms key in DURATION_UNITS object
Low Severity
The ms: 1 entry in DURATION_UNITS is never used. The 'ms' case is handled specially in parseDuration (lines 21-23) with hardcoded mult = 1, while DURATION_UNITS is only accessed for single-character units ('s', 'm', 'h'). Either remove the unused entry or use DURATION_UNITS['ms'] for consistency.


{ maxRequests, duration }. A request is sent only when every window allows one more; each window resets independently.resolves #55
Note
Medium Risk
Refactors the core request scheduling/timer logic and option validation, which could subtly change throttling behavior and timing under load; changes are well-covered by new regression tests.
Overview
Adds multi-window rate limiting via an optional
limitsarray ({ maxRequests, duration }), where a request only dispatches when all windows have capacity and each window resets on its own timer.Introduces a
durationoption (string like500ms,1s,2m,1hor a number for backward compatibility), tightens option validation/error messages, and ensuressetRateLimitOptionsclears old timers and can be called withnull/undefined.Updates
README.md,CHANGELOG.md, and TypeScript typings to document the new API, expands the test suite significantly for parsing/validation and multi-limit behavior, and narrows the CI Node.js matrix to20–25.Written by Cursor Bugbot for commit 406f7f2. This will update automatically on new commits. Configure here.