UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

127 lines 6.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueueRateLimit = void 0; const redis_smq_common_1 = require("redis-smq-common"); const redis_client_js_1 = require("../../common/redis-client/redis-client.js"); const scripts_js_1 = require("../../common/redis-client/scripts/scripts.js"); const redis_keys_js_1 = require("../../common/redis-keys/redis-keys.js"); const index_js_1 = require("../../config/index.js"); const _parse_queue_params_and_validate_js_1 = require("../queue/_/_parse-queue-params-and-validate.js"); const index_js_2 = require("../queue/index.js"); const _has_rate_limit_exceeded_js_1 = require("./_/_has-rate-limit-exceeded.js"); const index_js_3 = require("./errors/index.js"); class QueueRateLimit { constructor() { this.shutdown = (cb) => { redis_smq_common_1.async.waterfall([this.queue.shutdown, this.redisClient.shutdown], cb); }; this.logger = redis_smq_common_1.logger.getLogger(index_js_1.Configuration.getSetConfig().logger, `queue-rate-limit`); this.redisClient = new redis_client_js_1.RedisClient(); this.redisClient.on('error', (err) => this.logger.error(err)); this.queue = new index_js_2.Queue(); } clear(queue, cb) { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else (0, _parse_queue_params_and_validate_js_1._parseQueueParamsAndValidate)(client, queue, (err, queueParams) => { if (err) cb(err); else if (!queueParams) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else { const { keyQueueProperties, keyQueueRateLimitCounter } = redis_keys_js_1.redisKeys.getQueueKeys(queueParams, null); const multi = client.multi(); multi.hdel(keyQueueProperties, String(index_js_2.EQueueProperty.RATE_LIMIT)); multi.del(keyQueueRateLimitCounter); multi.exec((err) => cb(err)); } }); }); } set(queue, rateLimit, cb) { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else (0, _parse_queue_params_and_validate_js_1._parseQueueParamsAndValidate)(client, queue, (err, queueParams) => { if (err) cb(err); else if (!queueParams) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else { const limit = Number(rateLimit.limit); if (isNaN(limit) || limit <= 0) { cb(new index_js_3.QueueRateLimitInvalidLimitError()); } const interval = Number(rateLimit.interval); if (isNaN(interval) || interval < 1000) { cb(new index_js_3.QueueRateLimitInvalidIntervalError()); } const validatedRateLimit = { interval, limit }; const { keyQueueProperties } = redis_keys_js_1.redisKeys.getQueueKeys(queueParams, null); client.runScript(scripts_js_1.ELuaScriptName.SET_QUEUE_RATE_LIMIT, [keyQueueProperties], [index_js_2.EQueueProperty.RATE_LIMIT, JSON.stringify(validatedRateLimit)], (err, reply) => { if (err) cb(err); else if (reply !== 'OK') cb(new index_js_3.QueueRateLimitQueueNotFoundError()); else cb(); }); } }); }); } hasExceeded(queue, rateLimit, cb) { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else (0, _parse_queue_params_and_validate_js_1._parseQueueParamsAndValidate)(client, queue, (err, queueParams) => { if (err) cb(err); else if (!queueParams) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else (0, _has_rate_limit_exceeded_js_1._hasRateLimitExceeded)(client, queueParams, rateLimit, cb); }); }); } get(queue, cb) { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else (0, _parse_queue_params_and_validate_js_1._parseQueueParamsAndValidate)(client, queue, (err, queueParams) => { if (err) cb(err); else if (!queueParams) cb(new redis_smq_common_1.CallbackEmptyReplyError()); else { const { keyQueueProperties } = redis_keys_js_1.redisKeys.getQueueKeys(queueParams, null); client.hget(keyQueueProperties, String(index_js_2.EQueueProperty.RATE_LIMIT), (err, reply) => { if (err) cb(err); else if (!reply) cb(null, null); else { const rateLimit = JSON.parse(reply); cb(null, rateLimit); } }); } }); }); } } exports.QueueRateLimit = QueueRateLimit; //# sourceMappingURL=queue-rate-limit.js.map