Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/core/task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

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 rateLimitSeconds to match the configuration property name (apiConfiguration?.rateLimitSeconds). This would make the code more consistent and self-documenting.

if (Task.lastGlobalApiRequestTime && rateLimit > 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional? While checking for rateLimit > 0 correctly handles the undefined/0 case, what happens if someone accidentally configures a negative rate limit? Should we add validation to ensure rateLimit >= 0 or use Math.max(0, rateLimit) to be defensive?

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we improve readability by combining these conditions? Something like:

Suggested change
if (remainingDelay > 0) {
if (Task.lastGlobalApiRequestTime && rateLimit > 0) {
const now = Date.now()
const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
const remainingDelay = rateLimit * 1000 - timeSinceLastRequest
rateLimitDelay = remainingDelay > 0 ? Math.ceil(remainingDelay / 1000) : 0
}

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.
Expand Down
Loading