|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 12 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 13 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 14 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 15 | + function step(op) { |
| 16 | + if (f) throw new TypeError("Generator is already executing."); |
| 17 | + while (_) try { |
| 18 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 19 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 20 | + switch (op[0]) { |
| 21 | + case 0: case 1: t = op; break; |
| 22 | + case 4: _.label++; return { value: op[1], done: false }; |
| 23 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 24 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 25 | + default: |
| 26 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 27 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 28 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 29 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 30 | + if (t[2]) _.ops.pop(); |
| 31 | + _.trys.pop(); continue; |
| 32 | + } |
| 33 | + op = body.call(thisArg, _); |
| 34 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 35 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 36 | + } |
| 37 | +}; |
| 38 | +var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } |
| 39 | +var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { |
| 40 | + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); |
| 41 | + var g = generator.apply(thisArg, _arguments || []), i, q = []; |
| 42 | + return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; |
| 43 | + function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } |
| 44 | + function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } |
| 45 | + function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } |
| 46 | + function fulfill(value) { resume("next", value); } |
| 47 | + function reject(value) { resume("throw", value); } |
| 48 | + function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } |
| 49 | +}; |
| 50 | +var __spreadArrays = (this && this.__spreadArrays) || function () { |
| 51 | + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; |
| 52 | + for (var r = Array(s), k = 0, i = 0; i < il; i++) |
| 53 | + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) |
| 54 | + r[k] = a[j]; |
| 55 | + return r; |
| 56 | +}; |
| 57 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 58 | +var util_1 = require("util"); |
| 59 | +var CallableBalancer = /** @class */ (function () { |
| 60 | + /** |
| 61 | + * |
| 62 | + * @param methods not empty array of functions |
| 63 | + * @param redisClient |
| 64 | + */ |
| 65 | + function CallableBalancer(methods, redisClient) { |
| 66 | + this._STORE_PREFIX = 'balancer'; |
| 67 | + this.INC_VALUE = 1; |
| 68 | + this._redisClient = redisClient; |
| 69 | + this._methods = methods; |
| 70 | + this._storeKey = this.makeStoreKey(methods); |
| 71 | + // Initialize Redis functions as async await |
| 72 | + this._functions = { |
| 73 | + delAsync: util_1.promisify(redisClient.DEL).bind(this._redisClient), |
| 74 | + zAddAsync: util_1.promisify(redisClient.ZADD).bind(this._redisClient), |
| 75 | + zRangeAsync: util_1.promisify(redisClient.zrange).bind(this._redisClient), |
| 76 | + zIncRbyAsync: util_1.promisify(redisClient.zincrby).bind(this._redisClient), |
| 77 | + }; |
| 78 | + } |
| 79 | + CallableBalancer.prototype.setMethods = function (methods) { |
| 80 | + this._methods = methods; |
| 81 | + this._storeKey = this.makeStoreKey(methods); |
| 82 | + }; |
| 83 | + CallableBalancer.prototype.increaseMethodRank = function (method, incValue) { |
| 84 | + if (incValue === void 0) { incValue = this.INC_VALUE; } |
| 85 | + return __awaiter(this, void 0, void 0, function () { |
| 86 | + return __generator(this, function (_a) { |
| 87 | + switch (_a.label) { |
| 88 | + case 0: return [4 /*yield*/, this._functions.zIncRbyAsync(this._storeKey, incValue, method.name)]; |
| 89 | + case 1: |
| 90 | + _a.sent(); |
| 91 | + return [2 /*return*/]; |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | + }; |
| 96 | + CallableBalancer.prototype.getAsyncIterator = function () { |
| 97 | + return __asyncGenerator(this, arguments, function getAsyncIterator_1() { |
| 98 | + var storedMethodNames, _i, storedMethodNames_1, methodName, _a, _b, method; |
| 99 | + return __generator(this, function (_c) { |
| 100 | + switch (_c.label) { |
| 101 | + case 0: return [4 /*yield*/, __await(this.getRange())]; |
| 102 | + case 1: |
| 103 | + storedMethodNames = _c.sent(); |
| 104 | + _i = 0, storedMethodNames_1 = storedMethodNames; |
| 105 | + _c.label = 2; |
| 106 | + case 2: |
| 107 | + if (!(_i < storedMethodNames_1.length)) return [3 /*break*/, 9]; |
| 108 | + methodName = storedMethodNames_1[_i]; |
| 109 | + _a = 0, _b = this._methods; |
| 110 | + _c.label = 3; |
| 111 | + case 3: |
| 112 | + if (!(_a < _b.length)) return [3 /*break*/, 8]; |
| 113 | + method = _b[_a]; |
| 114 | + if (!(method.name === methodName)) return [3 /*break*/, 7]; |
| 115 | + return [4 /*yield*/, __await(this.increaseMethodRank(method, this.INC_VALUE))]; |
| 116 | + case 4: |
| 117 | + _c.sent(); |
| 118 | + return [4 /*yield*/, __await(method)]; |
| 119 | + case 5: return [4 /*yield*/, _c.sent()]; |
| 120 | + case 6: |
| 121 | + _c.sent(); |
| 122 | + _c.label = 7; |
| 123 | + case 7: |
| 124 | + _a++; |
| 125 | + return [3 /*break*/, 3]; |
| 126 | + case 8: |
| 127 | + _i++; |
| 128 | + return [3 /*break*/, 2]; |
| 129 | + case 9: return [2 /*return*/]; |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | + }; |
| 134 | + /** |
| 135 | + * Clear store |
| 136 | + */ |
| 137 | + CallableBalancer.prototype.resetStore = function () { |
| 138 | + return __awaiter(this, void 0, void 0, function () { |
| 139 | + return __generator(this, function (_a) { |
| 140 | + switch (_a.label) { |
| 141 | + case 0: return [4 /*yield*/, this._functions.delAsync(this._storeKey)]; |
| 142 | + case 1: |
| 143 | + _a.sent(); |
| 144 | + return [2 /*return*/]; |
| 145 | + } |
| 146 | + }); |
| 147 | + }); |
| 148 | + }; |
| 149 | + CallableBalancer.prototype.getStoreKey = function () { |
| 150 | + return this._storeKey; |
| 151 | + }; |
| 152 | + /** |
| 153 | + * Return redis key to store list of methods with ranks |
| 154 | + * @param methods |
| 155 | + * @protected |
| 156 | + */ |
| 157 | + CallableBalancer.prototype.makeStoreKey = function (methods) { |
| 158 | + var storeKeyArray = [this._STORE_PREFIX]; |
| 159 | + methods.forEach(function (method) { |
| 160 | + storeKeyArray.push(method.name); |
| 161 | + }); |
| 162 | + return storeKeyArray.join('.'); |
| 163 | + }; |
| 164 | + /** |
| 165 | + * Returns an Array stored in Redis in Rank order |
| 166 | + * @private |
| 167 | + */ |
| 168 | + CallableBalancer.prototype.getRange = function () { |
| 169 | + return __awaiter(this, void 0, void 0, function () { |
| 170 | + var storedMethodNames, args_1, result_1; |
| 171 | + var _a; |
| 172 | + return __generator(this, function (_b) { |
| 173 | + switch (_b.label) { |
| 174 | + case 0: return [4 /*yield*/, this._functions.zRangeAsync(this._storeKey, 0, -1)]; |
| 175 | + case 1: |
| 176 | + storedMethodNames = _b.sent(); |
| 177 | + if (!(storedMethodNames.length !== this._methods.length)) return [3 /*break*/, 3]; |
| 178 | + args_1 = [], result_1 = []; |
| 179 | + this._methods.forEach(function (method) { |
| 180 | + // Default rank is 1 |
| 181 | + args_1.push("1", method.name); |
| 182 | + result_1.push(method.name); |
| 183 | + }); |
| 184 | + return [4 /*yield*/, (_a = this._functions).zAddAsync.apply(_a, __spreadArrays([this._storeKey, 'NX'], args_1))]; |
| 185 | + case 2: |
| 186 | + _b.sent(); |
| 187 | + return [2 /*return*/, result_1]; |
| 188 | + case 3: return [2 /*return*/, storedMethodNames]; |
| 189 | + } |
| 190 | + }); |
| 191 | + }); |
| 192 | + }; |
| 193 | + return CallableBalancer; |
| 194 | +}()); |
| 195 | +exports.default = CallableBalancer; |
0 commit comments