-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(queue-rate-limit): move QueueRateLimit.set() logic to LUA
- Loading branch information
Showing
9 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/common/redis-client/scripts/lua/set-queue-rate-limit.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
local keyQueueProperties = KEYS[1] | ||
|
||
--- | ||
|
||
local EQueuePropertyRateLimit = ARGV[1] | ||
local rateLimit = ARGV[2] | ||
|
||
local result = redis.call("EXISTS", keyQueueProperties) | ||
if result == 0 then | ||
return 'QUEUE_NOT_FOUND'; | ||
end | ||
|
||
redis.call("HSET", keyQueueProperties, EQueuePropertyRateLimit, rateLimit) | ||
return 'OK' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/lib/queue-rate-limit/errors/queue-rate-limit-queue-not-found.error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { QueueRateLimitError } from './queue-rate-limit.error.js'; | ||
|
||
export class QueueRateLimitQueueNotFoundError extends QueueRateLimitError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { jest } from '@jest/globals'; | ||
import { TFunction } from 'redis-smq-common'; | ||
|
||
export function mockModule(moduleName: string, mockFactory: TFunction) { | ||
if (process.env['NODE_OPTIONS']?.includes('--experimental-vm-modules')) { | ||
jest.unstable_mockModule(moduleName, mockFactory); | ||
} else { | ||
// Fixing TypeError: The second argument of `jest.mock` must be an inline function. | ||
jest.mock(moduleName, (...args: unknown[]) => mockFactory(...args)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { expect, test } from '@jest/globals'; | ||
import bluebird from 'bluebird'; | ||
import { resolve } from 'path'; | ||
import { getDirname, ICallback, IRedisClient } from 'redis-smq-common'; | ||
import { IQueueParams } from '../../../src/lib/queue/types/queue.js'; | ||
import { defaultQueue } from '../../common/message-producing-consuming.js'; | ||
import { mockModule } from '../../common/mock-module.js'; | ||
|
||
const dir = getDirname(); | ||
|
||
test('SetQueueRateLimit(): QueueRateLimitQueueNotFoundError', async () => { | ||
const modulePath = resolve( | ||
dir, | ||
'../../../src/lib/queue/_/_parse-queue-params-and-validate.js', | ||
); | ||
mockModule(modulePath, () => { | ||
return { | ||
_parseQueueParamsAndValidate( | ||
redisClient: IRedisClient, | ||
queue: string | IQueueParams, | ||
cb: ICallback<IQueueParams>, | ||
) { | ||
cb(null, defaultQueue); | ||
}, | ||
}; | ||
}); | ||
const { QueueRateLimit } = await import( | ||
'../../../src/lib/queue-rate-limit/queue-rate-limit.js' | ||
); | ||
const { QueueRateLimitQueueNotFoundError } = await import( | ||
'../../../src/lib/queue-rate-limit/errors/queue-rate-limit-queue-not-found.error.js' | ||
); | ||
const queueRateLimit = bluebird.promisifyAll(new QueueRateLimit()); | ||
await expect( | ||
queueRateLimit.setAsync(defaultQueue, { | ||
limit: 5, | ||
interval: 1000, | ||
}), | ||
).rejects.toThrow(QueueRateLimitQueueNotFoundError); | ||
await queueRateLimit.shutdownAsync(); | ||
}); |