UNPKG

@splitsoftware/splitio-commons

Version:
215 lines (214 loc) 10.6 kB
import { __spreadArray } from "tslib"; // Dynamically require ioredis to prevent strict TypeScript binding // and handle module export differences between v4 and v5. var RedisConstructor; try { var ioredisLib = require('ioredis'); RedisConstructor = ioredisLib.default || ioredisLib; } catch (e) { // If we reach here, the peer dependency is missing throw new Error('ioredis is missing. Please install ioredis v4 or v5.'); } import { merge, isString } from '../../utils/lang'; import { thenable } from '../../utils/promise/thenable'; import { timeout } from '../../utils/promise/timeout'; import { setToArray } from '../../utils/lang/sets'; var LOG_PREFIX = 'storage:redis-adapter: '; // If we ever decide to fully wrap every method, there's a Commander.getBuiltinCommands from ioredis. var METHODS_TO_PROMISE_WRAP = ['set', 'exec', 'del', 'get', 'keys', 'sadd', 'srem', 'sismember', 'smembers', 'incr', 'decr', 'rpush', 'expire', 'mget', 'lrange', 'ltrim', 'hset', 'hincrby', 'popNRaw', 'hgetall', 'llen', 'hget']; var METHODS_TO_PROMISE_WRAP_EXEC = ['pipeline']; // Not part of the settings since it'll vary on each storage. We should be removing storage specific logic from elsewhere. var DEFAULT_OPTIONS = { connectionTimeout: 10000, operationTimeout: 5000 }; // Library specifics. var DEFAULT_LIBRARY_OPTIONS = { enableOfflineQueue: false, connectTimeout: DEFAULT_OPTIONS.connectionTimeout, lazyConnect: false, // CRITICAL: v5 defaults this to 0 (disabled), which breaks dynamic clusters. // v4 defaulted to 5000. We explicitly set it here to ensure v5 works like v4. slotsRefreshInterval: 5000, }; /** * Redis adapter on top of the library of choice (written with ioredis) for some extra control. * Refactored to use Composition instead of Inheritance to support both v4 and v5. */ var RedisAdapter = /** @class */ (function () { function RedisAdapter(log, storageSettings) { if (storageSettings === void 0) { storageSettings = {}; } var options = RedisAdapter._defineOptions(storageSettings); this.log = log; this._options = options; this._notReadyCommandsQueue = []; this._runningCommands = new Set(); // Instantiate the client using the dynamic constructor var librarySettings = RedisAdapter._defineLibrarySettings(options); this.client = new (RedisConstructor.bind.apply(RedisConstructor, __spreadArray([void 0], librarySettings, false)))(); this._listenToEvents(); this._setTimeoutWrappers(); this._setDisconnectWrapper(); } RedisAdapter.prototype.on = function (event, listener) { return this.client.on(event, listener); }; RedisAdapter.prototype._listenToEvents = function () { var _this = this; this.client.once('ready', function () { var commandsCount = _this._notReadyCommandsQueue ? _this._notReadyCommandsQueue.length : 0; _this.log.info(LOG_PREFIX + "Redis connection established. Queued commands: ".concat(commandsCount, ".")); _this._notReadyCommandsQueue && _this._notReadyCommandsQueue.forEach(function (queued) { _this.log.info(LOG_PREFIX + "Executing queued ".concat(queued.name, " command.")); queued.command().then(queued.resolve).catch(queued.reject); }); // After the SDK is ready for the first time we'll stop queueing commands. This is just so we can keep handling BUR for them. _this._notReadyCommandsQueue = undefined; }); this.client.once('close', function () { _this.log.info(LOG_PREFIX + 'Redis connection closed.'); }); }; RedisAdapter.prototype._setTimeoutWrappers = function () { var _this = this; var instance = this; // We pass `bindTarget` so pipeline execution is bound to the pipeline object, // while standard commands are bound to the client. var wrapCommand = function (originalMethod, methodName, bindTarget) { return function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } function commandWrapper() { instance.log.debug("".concat(LOG_PREFIX, "Executing ").concat(methodName, ".")); var result = originalMethod.apply(bindTarget, params); if (thenable(result)) { // For handling pending commands on disconnect, add to the set and remove once finished. // On sync commands there's no need, only thenables. instance._runningCommands.add(result); var cleanUpRunningCommandsCb = function () { instance._runningCommands.delete(result); }; // Both success and error remove from queue. result.then(cleanUpRunningCommandsCb, cleanUpRunningCommandsCb); return timeout(instance._options.operationTimeout, result).catch(function (err) { instance.log.error("".concat(LOG_PREFIX).concat(methodName, " operation threw an error or exceeded configured timeout of ").concat(instance._options.operationTimeout, "ms. Message: ").concat(err)); // Handling is not the adapter responsibility. throw err; }); } return result; } if (instance._notReadyCommandsQueue) { return new Promise(function (resolve, reject) { instance._notReadyCommandsQueue.unshift({ resolve: resolve, reject: reject, command: commandWrapper, name: methodName.toUpperCase() }); }); } else { return commandWrapper(); } }; }; // Wrap regular async methods to track timeouts and queue when Redis is not yet executing commands. METHODS_TO_PROMISE_WRAP.forEach(function (methodName) { var originalFn = _this.client[methodName]; _this[methodName] = wrapCommand(originalFn, methodName, _this.client); }); // Special handling for pipeline~like methods. We need to wrap the async trigger, which is exec, but return the Pipeline right away. METHODS_TO_PROMISE_WRAP_EXEC.forEach(function (methodName) { var originalFn = _this.client[methodName]; // "First level wrapper" to handle the sync execution and wrap async, queueing later if applicable. _this[methodName] = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var res = originalFn.apply(instance.client, args); var originalExec = res.exec; res.exec = wrapCommand(originalExec, "".concat(methodName, ".exec"), res); return res; }; }); }; RedisAdapter.prototype._setDisconnectWrapper = function () { var instance = this; var originalMethod = this.client.disconnect; this.disconnect = function disconnect() { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } setTimeout(function deferredDisconnect() { if (instance._runningCommands.size > 0) { instance.log.info(LOG_PREFIX + "Attempting to disconnect but there are ".concat(instance._runningCommands.size, " commands still waiting for resolution. Defering disconnection until those finish.")); Promise.all(setToArray(instance._runningCommands)) .then(function () { instance.log.debug(LOG_PREFIX + 'Pending commands finished successfully, disconnecting.'); originalMethod.apply(instance.client, params); }) .catch(function (e) { instance.log.warn(LOG_PREFIX + "Pending commands finished with error: ".concat(e, ". Proceeding with disconnection.")); originalMethod.apply(instance.client, params); }); } else { instance.log.debug(LOG_PREFIX + 'No commands pending execution, disconnect.'); // Nothing pending, just proceed. originalMethod.apply(instance.client, params); } }, 10); }; }; /** * Receives the options and returns an array of parameters for the ioredis constructor. * Keeping both redis setup options for backwards compatibility. */ RedisAdapter._defineLibrarySettings = function (options) { var opts = merge({}, DEFAULT_LIBRARY_OPTIONS); var result = [opts]; if (!isString(options.url)) { merge(opts, { host: options.host, port: options.port, db: options.db, password: options.pass }); } else { // If it IS the string URL, that'll be the first param for ioredis. result.unshift(options.url); } if (options.connectionTimeout) { merge(opts, { connectTimeout: options.connectionTimeout }); } if (options.tls) { merge(opts, { tls: options.tls }); } return result; }; /** * Parses the options into what we care about. */ RedisAdapter._defineOptions = function (_a) { var connectionTimeout = _a.connectionTimeout, operationTimeout = _a.operationTimeout, url = _a.url, host = _a.host, port = _a.port, db = _a.db, pass = _a.pass, tls = _a.tls; var parsedOptions = { connectionTimeout: connectionTimeout, operationTimeout: operationTimeout, url: url, host: host, port: port, db: db, pass: pass, tls: tls }; return merge({}, DEFAULT_OPTIONS, parsedOptions); }; return RedisAdapter; }()); export { RedisAdapter };