UNPKG

badman-core

Version:

badman-core is the layer what all the badman packages depended,or can be used as a tool kit.

56 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class AbstractLoadBalancer { constructor(pool, isWeight) { if (isWeight) { this.isWeight = isWeight; } else { this.isWeight = false; } if (isWeight) { this.prepareWeights(pool); } else { this.pool = pool; } this.currentIndex = 0; } poolSize() { return this.pool.length; } existedPool() { return this.pool; } prepareWeights(pool) { if (pool.length === 0) { throw new Error('cannot prepare a zero length pool'); } const preparedPool = []; pool.sort(function (a, b) { return b.weight - a.weight; }); pool.forEach(function (entry, index) { let v; // check this in a way that allows the use of zeros and "false" as object if (entry.value !== undefined && entry.value !== null) { v = entry.value; } if (v === undefined || v === null) { throw new Error('Please specify an object or a value (alias for object) property for entry in index ' + index); } if (entry.weight <= 0) { throw new Error('Weight in index ' + index + ' must be greater than zero'); } if (entry.weight % 1 !== 0) { throw new Error('Weight in index ' + index + ' must be an integer'); } for (let i = 0; i < entry.weight; i++) { preparedPool.push(v); } }); return preparedPool; } } exports.default = AbstractLoadBalancer; //# sourceMappingURL=AbstractLoadBalancer.js.map