Skip to content

Conversation

@aishek
Copy link
Owner

@aishek aishek commented Jan 31, 2026

  • 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?.

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 limits array ({ maxRequests, duration }), where a request only dispatches when all windows have capacity and each window resets on its own timer.

Introduces a duration option (string like 500ms, 1s, 2m, 1h or a number for backward compatibility), tightens option validation/error messages, and ensures setRateLimitOptions clears old timers and can be called with null/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 to 20–25.

Written by Cursor Bugbot for commit 406f7f2. This will update automatically on new commits. Configure here.

@aishek aishek force-pushed the 55-multiple-limits-duration-parameter branch from 4ce49f3 to f81b937 Compare January 31, 2026 13:57
- 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.
@aishek aishek force-pushed the 55-multiple-limits-duration-parameter branch from f81b937 to b1879c4 Compare January 31, 2026 14:04
@aishek aishek self-assigned this Jan 31, 2026
@aishek aishek added the enhancement New feature or request label Jan 31, 2026
Aleksandr Borisov added 8 commits February 1, 2026 10:44
- 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.
Aleksandr Borisov added 2 commits February 1, 2026 22:35
- 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.
Copy link

@cursor cursor bot left a 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 }
Copy link

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.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple limits on the same instance (max per min and max per sec)

2 participants