n8n
Version:
n8n Workflow Automation Tool
128 lines • 5.22 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisClientService = void 0;
const typedi_1 = require("typedi");
const Logger_1 = require("../../Logger");
const ioredis_1 = __importDefault(require("ioredis"));
const OnShutdown_1 = require("../../decorators/OnShutdown");
const constants_1 = require("../../constants");
const config_1 = require("@n8n/config");
let RedisClientService = class RedisClientService {
constructor(logger, globalConfig) {
this.logger = logger;
this.globalConfig = globalConfig;
this.clients = new Set();
}
createClient(arg) {
const client = this.clusterNodes().length > 0
? this.createClusterClient(arg)
: this.createRegularClient(arg);
this.clients.add(client);
return client;
}
disconnectClients() {
for (const client of this.clients) {
client.disconnect();
}
}
toValidPrefix(prefix) {
if (this.clusterNodes().length > 0) {
if (!prefix.startsWith('{'))
prefix = '{' + prefix;
if (!prefix.endsWith('}'))
prefix += '}';
}
return prefix;
}
createRegularClient({ type, extraOptions, }) {
const options = this.getOptions({ extraOptions });
const { host, port } = this.globalConfig.queue.bull.redis;
options.host = host;
options.port = port;
this.logger.debug('[Redis] Initializing regular client', { type, host, port });
return new ioredis_1.default(options);
}
createClusterClient({ type, extraOptions, }) {
const options = this.getOptions({ extraOptions });
const clusterNodes = this.clusterNodes();
this.logger.debug('[Redis] Initializing cluster client', { type, clusterNodes });
return new ioredis_1.default.Cluster(clusterNodes, {
redisOptions: options,
clusterRetryStrategy: this.retryStrategy(),
});
}
getOptions({ extraOptions }) {
const { username, password, db, tls } = this.globalConfig.queue.bull.redis;
const options = {
username,
password,
db,
enableReadyCheck: false,
maxRetriesPerRequest: null,
retryStrategy: this.retryStrategy(),
...extraOptions,
};
if (tls)
options.tls = {};
return options;
}
retryStrategy() {
const RETRY_INTERVAL = 500;
const RESET_LENGTH = 30000;
const MAX_TIMEOUT = this.globalConfig.queue.bull.redis.timeoutThreshold;
let lastAttemptTs = 0;
let cumulativeTimeout = 0;
return () => {
const nowTs = Date.now();
if (nowTs - lastAttemptTs > RESET_LENGTH) {
cumulativeTimeout = 0;
lastAttemptTs = nowTs;
}
else {
cumulativeTimeout += nowTs - lastAttemptTs;
lastAttemptTs = nowTs;
if (cumulativeTimeout > MAX_TIMEOUT) {
this.logger.error(`[Redis] Unable to connect after max timeout of ${MAX_TIMEOUT} ms`);
this.logger.error('Exiting process...');
process.exit(1);
}
}
this.logger.warn('Redis unavailable - trying to reconnect...');
return RETRY_INTERVAL;
};
}
clusterNodes() {
return this.globalConfig.queue.bull.redis.clusterNodes
.split(',')
.filter((pair) => pair.trim().length > 0)
.map((pair) => {
const [host, port] = pair.split(':');
return { host, port: parseInt(port) };
});
}
};
exports.RedisClientService = RedisClientService;
__decorate([
(0, OnShutdown_1.OnShutdown)(constants_1.LOWEST_SHUTDOWN_PRIORITY),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], RedisClientService.prototype, "disconnectClients", null);
exports.RedisClientService = RedisClientService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
config_1.GlobalConfig])
], RedisClientService);
//# sourceMappingURL=redis-client.service.js.map
;