eyevinn-channel-engine 4.3.12 → 5.0.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -148,7 +148,7 @@ var PlayheadStateStore = /** @class */ (function (_super) {
148
148
  function PlayheadStateStore(opts) {
149
149
  return _super.call(this, "playhead", opts, {
150
150
  state: PlayheadState.IDLE,
151
- tickInterval: 3,
151
+ tickInterval: opts ? (opts.averageSegmentDuration / 1000) : 3,
152
152
  mediaSeq: 0,
153
153
  vodMediaSeqVideo: 0,
154
154
  vodMediaSeqAudio: 0,
@@ -3,7 +3,10 @@ declare class RedisStateStore {
3
3
  constructor(keyPrefix: any, opts: any);
4
4
  keyPrefix: any;
5
5
  volatileKeyTTL: any;
6
- client: any;
6
+ pool: any[];
7
+ createRedisPool(size: any, redisUrl: any): any[];
8
+ getClientFromPool(maxWaitTime?: number): Promise<any>;
9
+ returnClientToPool(client: any): void;
7
10
  initAsync(id: any, initData: any): Promise<{}>;
8
11
  resetAsync(id: any, initData: any): Promise<void>;
9
12
  resetAllAsync(): Promise<void>;
@@ -12,5 +15,5 @@ declare class RedisStateStore {
12
15
  setValues(id: any, data: any): Promise<{}>;
13
16
  setAsync(id: any, key: any, value: any): Promise<any>;
14
17
  setVolatileAsync(id: any, key: any, value: any): Promise<any>;
15
- removeAsync(id: any, key: any): Promise<void>;
18
+ removeAsync(id: any, key: any): Promise<any>;
16
19
  }
@@ -36,13 +36,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  };
37
37
  var Redis = require("ioredis");
38
38
  var debug = require("debug")("redis-state-store");
39
- var cloudWatchLog = require("./util.js").cloudWatchLog;
39
+ var _a = require("./util.js"), cloudWatchLog = _a.cloudWatchLog, timer = _a.timer;
40
40
  var DEFAULT_VOLATILE_KEY_TTL = 5; // Timeout so it should not expire within one normal increment iteration (in seconds)
41
41
  function isTrue(s) {
42
42
  var regex = /^\s*(true|1)\s*$/i;
43
43
  return regex.test(s);
44
44
  }
45
45
  var REDIS_VERBOSE_LOG = process.env.REDIS_VERBOSE_LOG ? isTrue(process.env.REDIS_VERBOSE_LOG) : false;
46
+ var REDIS_POOL_SIZE = process.env.REDIS_POOL_SIZE ? parseInt(process.env.REDIS_POOL_SIZE) : 15;
46
47
  var RedisStateStore = /** @class */ (function () {
47
48
  function RedisStateStore(keyPrefix, opts) {
48
49
  this.keyPrefix = keyPrefix;
@@ -56,8 +57,42 @@ var RedisStateStore = /** @class */ (function () {
56
57
  debug("Overriding default, volatileKeyTTL=".concat(opts.volatileKeyTTL, "s"));
57
58
  this.volatileKeyTTL = opts.volatileKeyTTL;
58
59
  }
59
- this.client = new Redis(opts.redisUrl, { enableAutoPipelining: true });
60
+ this.pool = this.createRedisPool(REDIS_POOL_SIZE, opts.redisUrl);
60
61
  }
62
+ RedisStateStore.prototype.createRedisPool = function (size, redisUrl) {
63
+ var pool = [];
64
+ for (var i = 0; i < size; i++) {
65
+ var client = new Redis(redisUrl);
66
+ pool.push(client);
67
+ }
68
+ return pool;
69
+ };
70
+ RedisStateStore.prototype.getClientFromPool = function (maxWaitTime) {
71
+ if (maxWaitTime === void 0) { maxWaitTime = 5000; }
72
+ return __awaiter(this, void 0, void 0, function () {
73
+ var startTime;
74
+ return __generator(this, function (_a) {
75
+ switch (_a.label) {
76
+ case 0:
77
+ startTime = Date.now();
78
+ _a.label = 1;
79
+ case 1:
80
+ if (!(this.pool.length === 0)) return [3 /*break*/, 3];
81
+ if (Date.now() - startTime > maxWaitTime) {
82
+ console.error("[!] Timeout: No available Redis clients in the pool");
83
+ }
84
+ return [4 /*yield*/, timer(100)];
85
+ case 2:
86
+ _a.sent();
87
+ return [3 /*break*/, 1];
88
+ case 3: return [2 /*return*/, this.pool.pop()];
89
+ }
90
+ });
91
+ });
92
+ };
93
+ RedisStateStore.prototype.returnClientToPool = function (client) {
94
+ this.pool.push(client);
95
+ };
61
96
  RedisStateStore.prototype.initAsync = function (id, initData) {
62
97
  return __awaiter(this, void 0, void 0, function () {
63
98
  var isInitiated, data, _i, _a, key, _b, _c;
@@ -115,64 +150,69 @@ var RedisStateStore = /** @class */ (function () {
115
150
  };
116
151
  RedisStateStore.prototype.resetAllAsync = function () {
117
152
  return __awaiter(this, void 0, void 0, function () {
118
- var resetAsync;
119
- var _this = this;
153
+ var client, err_1;
120
154
  return __generator(this, function (_a) {
121
155
  switch (_a.label) {
122
- case 0:
123
- resetAsync = new Promise(function (resolve, reject) {
124
- _this.client.flushall(function (err, reply) {
125
- if (!err) {
126
- console.log("Flushed Redis db: ", reply);
127
- resolve();
128
- }
129
- else {
130
- reject(err);
131
- }
132
- });
133
- });
134
- return [4 /*yield*/, resetAsync];
156
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
135
157
  case 1:
158
+ client = _a.sent();
159
+ _a.label = 2;
160
+ case 2:
161
+ _a.trys.push([2, 4, 5, 6]);
162
+ return [4 /*yield*/, client.flushall()];
163
+ case 3:
136
164
  _a.sent();
137
- return [2 /*return*/];
165
+ console.log("Flushed Redis db");
166
+ return [3 /*break*/, 6];
167
+ case 4:
168
+ err_1 = _a.sent();
169
+ console.error("Error flushing Redis db:", err_1);
170
+ return [3 /*break*/, 6];
171
+ case 5:
172
+ this.returnClientToPool(client);
173
+ return [7 /*endfinally*/];
174
+ case 6: return [2 /*return*/];
138
175
  }
139
176
  });
140
177
  });
141
178
  };
142
179
  RedisStateStore.prototype.getValues = function (id, keys) {
143
180
  return __awaiter(this, void 0, void 0, function () {
144
- var pipeline, data, startMs, _loop_1, this_1, _i, keys_1, key, ops, ioTimeMs;
181
+ var client, pipeline, data, startMs, _i, keys_1, key, storeKey, results, ops, ioTimeMs;
182
+ var _this = this;
145
183
  return __generator(this, function (_a) {
146
184
  switch (_a.label) {
147
- case 0:
148
- pipeline = this.client.pipeline();
185
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
186
+ case 1:
187
+ client = _a.sent();
188
+ pipeline = client.pipeline();
149
189
  data = {};
150
190
  startMs = Date.now();
151
- _loop_1 = function (key) {
152
- var storeKey = "" + this_1.keyPrefix + id + key;
153
- pipeline.get(storeKey, function (err, reply) {
154
- if (!err) {
155
- debug("REDIS get(pipeline) ".concat(storeKey, ":").concat(reply ? reply.length + " chars" : "null"));
156
- try {
157
- data[key] = JSON.parse(reply);
158
- }
159
- catch (err) {
160
- console.error("REDIS get(pipeline): Failed to parse ".concat(storeKey, " data: '").concat(reply, "'"));
161
- }
162
- }
163
- });
164
- };
165
- this_1 = this;
166
191
  for (_i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
167
192
  key = keys_1[_i];
168
- _loop_1(key);
193
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
194
+ pipeline.get(storeKey);
169
195
  }
170
- ops = pipeline.length;
171
196
  return [4 /*yield*/, pipeline.exec()];
172
- case 1:
173
- _a.sent();
197
+ case 2:
198
+ results = _a.sent();
199
+ ops = pipeline.length;
200
+ results.forEach(function (result, index) {
201
+ var reply = result[1];
202
+ var storeKey = "".concat(_this.keyPrefix).concat(id).concat(keys[index]);
203
+ debug("REDIS get(pipeline) ".concat(storeKey, ":").concat(reply ? reply.length + " chars" : "null"));
204
+ if (reply) {
205
+ try {
206
+ data[keys[index]] = JSON.parse(reply);
207
+ }
208
+ catch (err) {
209
+ console.error("REDIS get(pipeline): Failed to parse ".concat(storeKey, " data: '").concat(reply, "'"));
210
+ }
211
+ }
212
+ });
174
213
  ioTimeMs = Date.now() - startMs;
175
- cloudWatchLog(!REDIS_VERBOSE_LOG, 'redis', { event: 'getValues', operations: ops, ioTimeMs: ioTimeMs });
214
+ cloudWatchLog(!REDIS_VERBOSE_LOG, "redis", { event: "getValues", operations: ops, ioTimeMs: ioTimeMs });
215
+ this.returnClientToPool(client);
176
216
  return [2 /*return*/, data];
177
217
  }
178
218
  });
@@ -180,71 +220,64 @@ var RedisStateStore = /** @class */ (function () {
180
220
  };
181
221
  RedisStateStore.prototype.getAsync = function (id, key) {
182
222
  return __awaiter(this, void 0, void 0, function () {
183
- var startMs, storeKey, getAsync, data;
184
- var _this = this;
223
+ var client, startMs, storeKey, reply, ioTimeMs;
185
224
  return __generator(this, function (_a) {
186
225
  switch (_a.label) {
187
- case 0:
188
- startMs = Date.now();
189
- storeKey = "" + this.keyPrefix + id + key;
190
- getAsync = new Promise(function (resolve, reject) {
191
- _this.client.get(storeKey, function (err, reply) {
192
- var ioTimeMs = Date.now() - startMs;
193
- debug("REDIS get ".concat(storeKey, ":").concat(reply ? reply.length + " chars" : "null", " (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? 'REDISSLOW!' : ''));
194
- if (!err) {
195
- var data_1;
196
- try {
197
- data_1 = JSON.parse(reply);
198
- }
199
- catch (err) {
200
- console.error("REDIS get: Failed to parse ".concat(storeKey, " data: '").concat(reply, "'"));
201
- }
202
- cloudWatchLog(!REDIS_VERBOSE_LOG, 'redis', { event: 'get', operations: 1, ioTimeMs: ioTimeMs });
203
- resolve(data_1);
204
- }
205
- else {
206
- reject(err);
207
- }
208
- });
209
- });
210
- return [4 /*yield*/, getAsync];
226
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
211
227
  case 1:
212
- data = _a.sent();
213
- return [2 /*return*/, data];
228
+ client = _a.sent();
229
+ startMs = Date.now();
230
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
231
+ return [4 /*yield*/, client.get(storeKey)];
232
+ case 2:
233
+ reply = _a.sent();
234
+ ioTimeMs = Date.now() - startMs;
235
+ debug("REDIS get ".concat(storeKey, ":").concat(reply ? reply.length + " chars" : "null", " (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? "REDISSLOW!" : ""));
236
+ this.returnClientToPool(client);
237
+ if (reply) {
238
+ try {
239
+ cloudWatchLog(!REDIS_VERBOSE_LOG, "redis", { event: "get", operations: 1, ioTimeMs: ioTimeMs });
240
+ return [2 /*return*/, JSON.parse(reply)];
241
+ }
242
+ catch (err) {
243
+ console.error("REDIS get: Failed to parse ".concat(storeKey, " data: '").concat(reply, "'"));
244
+ }
245
+ }
246
+ return [2 /*return*/, null];
214
247
  }
215
248
  });
216
249
  });
217
250
  };
218
251
  RedisStateStore.prototype.setValues = function (id, data) {
219
252
  return __awaiter(this, void 0, void 0, function () {
220
- var returnData, startMs, pipeline, _loop_2, this_2, _i, _a, key, ops, ioTimeMs;
253
+ var client, returnData, startMs, pipeline, _i, _a, key, storeKey, value, results, ops, ioTimeMs;
254
+ var _this = this;
221
255
  return __generator(this, function (_b) {
222
256
  switch (_b.label) {
223
- case 0:
257
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
258
+ case 1:
259
+ client = _b.sent();
224
260
  returnData = {};
225
261
  startMs = Date.now();
226
- pipeline = this.client.pipeline();
227
- _loop_2 = function (key) {
228
- var storeKey = "" + this_2.keyPrefix + id + key;
229
- var value = data[key];
230
- pipeline.set(storeKey, JSON.stringify(value), function (err, res) {
231
- if (!err) {
232
- debug("REDIS set(pipeline) ".concat(storeKey, ": ").concat(res));
233
- returnData[key] = value;
234
- }
235
- });
236
- };
237
- this_2 = this;
262
+ pipeline = client.pipeline();
238
263
  for (_i = 0, _a = Object.keys(data); _i < _a.length; _i++) {
239
264
  key = _a[_i];
240
- _loop_2(key);
265
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
266
+ value = data[key];
267
+ pipeline.set(storeKey, JSON.stringify(value));
241
268
  }
242
- ops = pipeline.length;
243
269
  return [4 /*yield*/, pipeline.exec()];
244
- case 1:
245
- _b.sent();
270
+ case 2:
271
+ results = _b.sent();
272
+ ops = pipeline.length;
273
+ results.forEach(function (result, index) {
274
+ var storeKey = "".concat(_this.keyPrefix).concat(id).concat(Object.keys(data)[index]);
275
+ debug("REDIS set(pipeline) ".concat(storeKey, ": ").concat(result[1]));
276
+ returnData[Object.keys(data)[index]] = data[Object.keys(data)[index]];
277
+ });
246
278
  ioTimeMs = Date.now() - startMs;
247
- cloudWatchLog(!REDIS_VERBOSE_LOG, 'redis', { event: 'setValues', operations: ops, ioTimeMs: ioTimeMs });
279
+ cloudWatchLog(!REDIS_VERBOSE_LOG, "redis", { event: "setValues", operations: ops, ioTimeMs: ioTimeMs });
280
+ this.returnClientToPool(client);
248
281
  return [2 /*return*/, returnData];
249
282
  }
250
283
  });
@@ -252,56 +285,43 @@ var RedisStateStore = /** @class */ (function () {
252
285
  };
253
286
  RedisStateStore.prototype.setAsync = function (id, key, value) {
254
287
  return __awaiter(this, void 0, void 0, function () {
255
- var startMs, storeKey, setAsync;
256
- var _this = this;
288
+ var client, startMs, storeKey, res, ioTimeMs;
257
289
  return __generator(this, function (_a) {
258
290
  switch (_a.label) {
259
- case 0:
291
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
292
+ case 1:
293
+ client = _a.sent();
260
294
  startMs = Date.now();
261
- storeKey = "" + this.keyPrefix + id + key;
262
- setAsync = new Promise(function (resolve, reject) {
263
- _this.client.set(storeKey, JSON.stringify(value), function (err, res) {
264
- var ioTimeMs = Date.now() - startMs;
265
- debug("REDIS set ".concat(storeKey, ": ").concat(res, " (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? "REDISSLOW!" : ""));
266
- if (!err) {
267
- cloudWatchLog(!REDIS_VERBOSE_LOG, 'redis', { event: 'set', operations: 1, ioTimeMs: ioTimeMs });
268
- resolve(value);
269
- }
270
- else {
271
- reject(err);
272
- }
273
- });
274
- });
275
- return [4 /*yield*/, setAsync];
276
- case 1: return [2 /*return*/, _a.sent()];
295
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
296
+ return [4 /*yield*/, client.set(storeKey, JSON.stringify(value))];
297
+ case 2:
298
+ res = _a.sent();
299
+ ioTimeMs = Date.now() - startMs;
300
+ debug("REDIS set ".concat(storeKey, ": ").concat(res, " (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? "REDISSLOW!" : ""));
301
+ cloudWatchLog(!REDIS_VERBOSE_LOG, "redis", { event: "set", operations: 1, ioTimeMs: ioTimeMs });
302
+ this.returnClientToPool(client);
303
+ return [2 /*return*/, value];
277
304
  }
278
305
  });
279
306
  });
280
307
  };
281
308
  RedisStateStore.prototype.setVolatileAsync = function (id, key, value) {
282
309
  return __awaiter(this, void 0, void 0, function () {
283
- var data, storeKey, expireAsync;
284
- var _this = this;
310
+ var data, storeKey, client;
285
311
  return __generator(this, function (_a) {
286
312
  switch (_a.label) {
287
313
  case 0: return [4 /*yield*/, this.setAsync(id, key, value)];
288
314
  case 1:
289
315
  data = _a.sent();
290
- storeKey = "" + this.keyPrefix + id + key;
291
- expireAsync = new Promise(function (resolve, reject) {
292
- _this.client.expire(storeKey, _this.volatileKeyTTL, function (err, res) {
293
- if (!err) {
294
- debug("REDIS expire ".concat(storeKey, " ").concat(_this.volatileKeyTTL, "s: ").concat(res === 1 ? "OK" : "KEY DOES NOT EXIST"));
295
- resolve();
296
- }
297
- else {
298
- reject(err);
299
- }
300
- });
301
- });
302
- return [4 /*yield*/, expireAsync];
316
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
317
+ return [4 /*yield*/, this.getClientFromPool()];
303
318
  case 2:
319
+ client = _a.sent();
320
+ return [4 /*yield*/, client.expire(storeKey, this.volatileKeyTTL)];
321
+ case 3:
304
322
  _a.sent();
323
+ debug("REDIS expire ".concat(storeKey, " ").concat(this.volatileKeyTTL, "s"));
324
+ this.returnClientToPool(client);
305
325
  return [2 /*return*/, data];
306
326
  }
307
327
  });
@@ -309,30 +329,22 @@ var RedisStateStore = /** @class */ (function () {
309
329
  };
310
330
  RedisStateStore.prototype.removeAsync = function (id, key) {
311
331
  return __awaiter(this, void 0, void 0, function () {
312
- var startMs, storeKey, delAsync;
313
- var _this = this;
332
+ var client, startMs, storeKey, res, ioTimeMs;
314
333
  return __generator(this, function (_a) {
315
334
  switch (_a.label) {
316
- case 0:
317
- startMs = Date.now();
318
- storeKey = "" + this.keyPrefix + id + key;
319
- delAsync = new Promise(function (resolve, reject) {
320
- _this.client.del(storeKey, function (err, res) {
321
- var ioTimeMs = Date.now() - startMs;
322
- debug("REDIS remove ".concat(storeKey, ": (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? "REDISSLOW!" : ""));
323
- if (!err) {
324
- cloudWatchLog(!REDIS_VERBOSE_LOG, 'redis', { event: 'remove', operations: 1, ioTimeMs: ioTimeMs });
325
- resolve();
326
- }
327
- else {
328
- reject(err);
329
- }
330
- });
331
- });
332
- return [4 /*yield*/, delAsync];
335
+ case 0: return [4 /*yield*/, this.getClientFromPool()];
333
336
  case 1:
334
- _a.sent();
335
- return [2 /*return*/];
337
+ client = _a.sent();
338
+ startMs = Date.now();
339
+ storeKey = "".concat(this.keyPrefix).concat(id).concat(key);
340
+ return [4 /*yield*/, client.del(storeKey)];
341
+ case 2:
342
+ res = _a.sent();
343
+ ioTimeMs = Date.now() - startMs;
344
+ debug("REDIS remove ".concat(storeKey, ": (").concat(ioTimeMs, "ms) ").concat(ioTimeMs > 1000 ? "REDISSLOW!" : ""));
345
+ cloudWatchLog(!REDIS_VERBOSE_LOG, "redis", { event: "remove", operations: 1, ioTimeMs: ioTimeMs });
346
+ this.returnClientToPool(client);
347
+ return [2 /*return*/, res];
336
348
  }
337
349
  });
338
350
  });
@@ -171,11 +171,11 @@ export declare class ChannelEngine {
171
171
  getSubtitleManifests(channelId: any): Promise<{}>;
172
172
  _handleHeartbeat(req: any, res: any, next: any): void;
173
173
  _handleMasterManifest(req: any, res: any, next: any): Promise<void>;
174
+ _handleMediaManifest(req: any, res: any, next: any): Promise<void>;
174
175
  _handleAudioManifest(req: any, res: any, next: any): Promise<void>;
175
176
  _handleSubtitleManifest(req: any, res: any, next: any): Promise<void>;
176
177
  _handleDummySubtitleEndpoint(req: any, res: any, next: any): Promise<void>;
177
178
  _handleSubtitleSliceEndpoint(req: any, res: any, next: any): Promise<void>;
178
- _handleMediaManifest(req: any, res: any, next: any): Promise<void>;
179
179
  _handleEventStream(req: any, res: any, next: any): void;
180
180
  _handleStatus(req: any, res: any, next: any): Promise<void>;
181
181
  _handleAggregatedSessionHealth(req: any, res: any, next: any): Promise<void>;