UNPKG

@duongtrungnguyen/nestro

Version:
62 lines 2.14 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var weighted_round_robin_strategy_exports = {}; __export(weighted_round_robin_strategy_exports, { WeightedRoundRobinStrategy: () => WeightedRoundRobinStrategy }); module.exports = __toCommonJS(weighted_round_robin_strategy_exports); class WeightedRoundRobinStrategy { constructor() { this.counters = /* @__PURE__ */ new Map(); this.defaultWeight = 1; } selectInstance(instances) { if (!instances || instances.length === 0) { return null; } if (instances.length === 1) { return instances[0]; } const serviceName = instances[0].name; let totalWeight = 0; const weights = instances.map((instance) => { const weight = instance.metadata?.weight || this.defaultWeight; totalWeight += weight; return weight; }); let counter = this.counters.get(serviceName) || 0; counter = (counter + 1) % totalWeight; this.counters.set(serviceName, counter); let weightSum = 0; for (let i = 0; i < instances.length; i++) { weightSum += weights[i]; if (counter < weightSum) { return instances[i]; } } return instances[0]; } reset() { this.counters.clear(); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WeightedRoundRobinStrategy }); //# sourceMappingURL=weighted-round-robin.strategy.js.map