From 7c86d070c916df7f240842150e870a5a84b015a9 Mon Sep 17 00:00:00 2001 From: Uros Smolnik Date: Sat, 24 Feb 2024 14:33:47 -0800 Subject: [PATCH] perf(worker.ts): lowering the threshold at which blocking redis operation is used to fetch next job With constant delayed jobs in highly used queue (mostly processing at least one job), it can happen calculated `blockTimeout` is mostly smaller then current hardcoded threashold of 50ms. If this happens, worker run loop ends up looping with no awaits, causing high CPU usage on Redis and runtime itself. --- src/classes/worker.ts | 4 ++-- src/interfaces/advanced-options.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/classes/worker.ts b/src/classes/worker.ts index d929eb0069..32450d31cf 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -603,8 +603,8 @@ export class Worker< 0, ); - // Blocking for less than 50ms is useless. - if (blockTimeout > 0.05) { + // Blocking for less than 10ms is useless. + if (blockTimeout > this.opts.settings?.blockTimeoutThreshold ?? 0.01) { blockTimeout = this.blockingConnection.capabilities.canDoubleTimeout ? blockTimeout : Math.ceil(blockTimeout); diff --git a/src/interfaces/advanced-options.ts b/src/interfaces/advanced-options.ts index cb463caf18..8c709a642b 100644 --- a/src/interfaces/advanced-options.ts +++ b/src/interfaces/advanced-options.ts @@ -18,4 +18,14 @@ export interface AdvancedOptions extends AdvancedRepeatOptions { * A custom backoff strategy. */ backoffStrategy?: BackoffStrategy; + + /** + * Minimum blocking operation timeout in seconds used when fetching next job. + * If timeout would be smaller than this defined threshold - because of next + * delayed job would be available to be processed sooner - blocking operation + * will not be used. + * + * @defaultValue 0.01 + */ + blockTimeoutThreshold?: number; }