From 9760b85dfbcc9b3c744f616961ef939e8951321d Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Fri, 26 Apr 2024 22:11:56 -0600 Subject: [PATCH] perf(worker): do not call bzpopmin when blockDelay is lower or equal 0 (#2544) ref #2466 --- src/classes/worker.ts | 28 ++++++++++++++++------------ tests/test_worker.ts | 14 ++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/classes/worker.ts b/src/classes/worker.ts index 45c572f353..1a23f2dc90 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -621,20 +621,22 @@ will never work with more accuracy than 1ms. */ if (!this.closing) { let blockTimeout = this.getBlockTimeout(blockUntil); - blockTimeout = this.blockingConnection.capabilities.canDoubleTimeout - ? blockTimeout - : Math.ceil(blockTimeout); + if (blockTimeout > 0) { + blockTimeout = this.blockingConnection.capabilities.canDoubleTimeout + ? blockTimeout + : Math.ceil(blockTimeout); - this.updateDelays(); // reset delays to avoid reusing same values in next iteration - // 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); + this.updateDelays(); // reset delays to avoid reusing same values in next iteration + // 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); + } } } @@ -658,7 +660,9 @@ will never work with more accuracy than 1ms. */ if (blockUntil) { const blockDelay = blockUntil - Date.now(); // when we reach the time to get new jobs - if (blockDelay < this.minimumBlockTimeout * 1000) { + if (blockDelay <= 0) { + return blockDelay; + } else if (blockDelay < this.minimumBlockTimeout * 1000) { return this.minimumBlockTimeout; } else { // We restrict the maximum block timeout to 10 second to avoid diff --git a/tests/test_worker.ts b/tests/test_worker.ts index 6ebeadde28..4cc39ca466 100644 --- a/tests/test_worker.ts +++ b/tests/test_worker.ts @@ -949,7 +949,7 @@ describe('workers', function () { describe('when blockUntil is greater than 0', () => { describe('when blockUntil is lower than date now value', () => { - it('returns minimumBlockTimeout', async () => { + it('returns blockDelay value lower or equal 0', async () => { const worker = new Worker(queueName, async () => {}, { connection, prefix, @@ -957,15 +957,9 @@ describe('workers', function () { }); await worker.waitUntilReady(); - if (isRedisVersionLowerThan(worker.redisVersion, '7.0.8')) { - expect(worker['getBlockTimeout'](Date.now() - 1)).to.be.equal( - 0.002, - ); - } else { - expect(worker['getBlockTimeout'](Date.now() - 1)).to.be.equal( - 0.001, - ); - } + expect( + worker['getBlockTimeout'](Date.now() - 1), + ).to.be.lessThanOrEqual(0); await worker.close(); }); });