-
-
Notifications
You must be signed in to change notification settings - Fork 196
Allow changing intervalCap
later
#233
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: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -16,11 +16,9 @@ Promise queue with concurrency control. | |
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> { // eslint-disable-line @typescript-eslint/naming-convention | ||
readonly #carryoverConcurrencyCount: boolean; | ||
|
||
readonly #isIntervalIgnored: boolean; | ||
|
||
#intervalCount = 0; | ||
|
||
readonly #intervalCap: number; | ||
#intervalCap: number; | ||
|
||
#rateLimitedInInterval = false; | ||
#rateLimitFlushScheduled = false; | ||
|
@@ -58,6 +56,12 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |
*/ | ||
timeout?: number; | ||
|
||
static #assertValidIntervalCap(value: unknown): asserts value is number { | ||
if (!(typeof value === 'number' && value >= 1)) { | ||
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${value?.toString() ?? ''}\` (${typeof value})`); | ||
} | ||
} | ||
|
||
constructor(options?: Options<QueueType, EnqueueOptionsType>) { | ||
super(); | ||
|
||
|
@@ -72,16 +76,13 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |
...options, | ||
} as Options<QueueType, EnqueueOptionsType>; | ||
|
||
if (!(typeof options.intervalCap === 'number' && options.intervalCap >= 1)) { | ||
throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${options.intervalCap?.toString() ?? ''}\` (${typeof options.intervalCap})`); | ||
} | ||
PQueue.#assertValidIntervalCap(options.intervalCap); | ||
|
||
if (options.interval === undefined || !(Number.isFinite(options.interval) && options.interval >= 0)) { | ||
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${options.interval?.toString() ?? ''}\` (${typeof options.interval})`); | ||
} | ||
|
||
this.#carryoverConcurrencyCount = options.carryoverConcurrencyCount!; | ||
this.#isIntervalIgnored = options.intervalCap === Number.POSITIVE_INFINITY || options.interval === 0; | ||
this.#intervalCap = options.intervalCap; | ||
this.#interval = options.interval; | ||
this.#queue = new options.queueClass!(); | ||
|
@@ -98,6 +99,25 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |
this.#setupRateLimitTracking(); | ||
} | ||
|
||
get #isIntervalIgnored(): boolean { | ||
return this.#intervalCap === Number.POSITIVE_INFINITY || this.#interval === 0; | ||
} | ||
|
||
get intervalCap(): number { | ||
return this.#intervalCap; | ||
} | ||
|
||
set intervalCap(value: number) { | ||
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. Removing the cap mid-interval must claer ratelimit state. When setting |
||
if (value === this.#intervalCap) { | ||
return; | ||
} | ||
|
||
PQueue.#assertValidIntervalCap(value); | ||
|
||
this.#intervalCap = value; | ||
this.#processQueue(); | ||
} | ||
|
||
get #doesIntervalAllowAnother(): boolean { | ||
return this.#isIntervalIgnored || this.#intervalCount < this.#intervalCap; | ||
} | ||
|
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.
"Debt is not carried over into the next interval" is bit unclear. Say exactly what happens: