Skip to content
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

fix(worker): consider block timeouts less than 50ms #2455

Merged
merged 3 commits into from
Mar 1, 2024
Merged
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
56 changes: 33 additions & 23 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,37 +595,30 @@ export class Worker<
}

try {
const opts: WorkerOptions = <WorkerOptions>this.opts;

if (!this.closing) {
let blockTimeout = Math.max(
blockUntil ? (blockUntil - Date.now()) / 1000 : opts.drainDelay,
0,
);
let blockTimeout = this.getBlockTimeout(blockUntil);

// Blocking for less than 50ms is useless.
if (blockTimeout > 0.05) {
blockTimeout = this.blockingConnection.capabilities.canDoubleTimeout
? blockTimeout
: Math.ceil(blockTimeout);
blockTimeout = this.blockingConnection.capabilities.canDoubleTimeout
Copy link
Contributor

@manast manast Feb 28, 2024

Choose a reason for hiding this comment

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

Actually we must have a blockTimeout that is a positive integer. The reason being that a value of 0 represents infinity, and a negative number would yield an exception. So maybe 1ms would do it? i.e. replace 0.05 by 0.001

? blockTimeout
: Math.ceil(blockTimeout);

// We restrict the maximum block timeout to 10 second to avoid
// blocking the connection for too long in the case of reconnections
// reference: https://github.com/taskforcesh/bullmq/issues/1658
blockTimeout = Math.min(blockTimeout, maximumBlockTimeout);
// We restrict the maximum block timeout to 10 second to avoid
// blocking the connection for too long in the case of reconnections
// reference: https://github.com/taskforcesh/bullmq/issues/1658
blockTimeout = Math.min(blockTimeout, maximumBlockTimeout);

// Markers should only be used for un-blocking, so we will handle them in this
// function only.
const result = await bclient.bzpopmin(this.keys.marker, blockTimeout);
// Markers should only be used for un-blocking, so we will handle them in this
// function only.
const result = await bclient.bzpopmin(this.keys.marker, blockTimeout);

if (result) {
const [_key, member, score] = result;
if (result) {
const [_key, member, score] = result;

if (member) {
return parseInt(score);
}
if (member) {
return parseInt(score);
}
}

return 0;
}
} catch (error) {
Expand All @@ -641,6 +634,23 @@ export class Worker<
return Infinity;
}

protected getBlockTimeout(blockUntil: number): number {
const opts: WorkerOptions = <WorkerOptions>this.opts;

// when there are delayed jobs
if (blockUntil) {
const blockDelay = blockUntil - Date.now();
// when we reach the time to get new jobs
if (blockDelay < 1) {
return 0.001;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

looks like it will be only needed when blockDelay is less than 1 when getting a marker from a delayed job, wdyt @manast

} else {
return blockDelay / 1000;
}
} else {
return Math.max(opts.drainDelay, 0);
}
}

/**
*
* This function is exposed only for testing purposes.
Expand Down
Loading