-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: correct rate limiting calculation to prevent incorrect delay display #7771
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2528,11 +2528,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |||||||||||||||
|
|
||||||||||||||||
| // Use the shared timestamp so that subtasks respect the same rate-limit | ||||||||||||||||
| // window as their parent tasks. | ||||||||||||||||
| if (Task.lastGlobalApiRequestTime) { | ||||||||||||||||
| const rateLimit = apiConfiguration?.rateLimitSeconds || 0 | ||||||||||||||||
| if (Task.lastGlobalApiRequestTime && rateLimit > 0) { | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentional? While checking for |
||||||||||||||||
| const now = Date.now() | ||||||||||||||||
| const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime | ||||||||||||||||
| const rateLimit = apiConfiguration?.rateLimitSeconds || 0 | ||||||||||||||||
| rateLimitDelay = Math.ceil(Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000) | ||||||||||||||||
| const remainingDelay = rateLimit * 1000 - timeSinceLastRequest | ||||||||||||||||
| // Only apply rate limit if there's actually time remaining to wait | ||||||||||||||||
| if (remainingDelay > 0) { | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we improve readability by combining these conditions? Something like:
Suggested change
This would eliminate the nested if statement while maintaining the same logic. |
||||||||||||||||
| rateLimitDelay = Math.ceil(remainingDelay / 1000) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there. | ||||||||||||||||
|
|
||||||||||||||||
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.
Minor suggestion: Consider renaming this variable to
rateLimitSecondsto match the configuration property name (apiConfiguration?.rateLimitSeconds). This would make the code more consistent and self-documenting.