UNPKG

whale-nest-nacos

Version:

If you are a nest micro service architecture, you can use the SDK, so as to realize service discovery based on nacos, distributed load balancing strategy

106 lines 4.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConnectionPool = void 0; const getNacosClientGrpc_1 = require("../loadBalance/getNacosClientGrpc"); class ConnectionPool { constructor(grpcOptionsObject, limit = 3) { this.grpcOptionsObject = {}; this.serviceGrpcMap = new Map(); this.concurrentPool = []; this.taskQueue = []; // 任务队列 this.grpcOptionsObject = grpcOptionsObject; this.limit = limit; this.grpcOptionsKeyArray = Object.keys(grpcOptionsObject); this._grpcOptionsKeyArray = Object.keys(grpcOptionsObject); } ready() { if (this.grpcOptionsKeyArray.length < this.limit) { (this.grpcOptionsKeyArray || []).forEach((key, index) => { this.createTask(this.grpcOptionsObject[key], key); this.grpcOptionsKeyArray.splice(index, 1); }); } else { while (this.concurrentPool.length < this.limit) { if (this.grpcOptionsKeyArray.length) { let key = this.grpcOptionsKeyArray.shift(); this.createTask(this.grpcOptionsObject[key], key); } } } let race = Promise.race(this.concurrentPool); return this.run(race); } run(race) { return __awaiter(this, void 0, void 0, function* () { return race.then(() => { if (this.grpcOptionsKeyArray.length) { let key = this.grpcOptionsKeyArray.shift(); this.createTask(this.grpcOptionsObject[key], key); if (!this.grpcOptionsKeyArray.length) { return this.run(Promise.all(this.concurrentPool)); } return this.run(Promise.race(this.concurrentPool)); } else { console.log(`连接池创建成功!连接总数:${this.serviceGrpcMap.size}`); return this.serviceGrpcMap; } }); }); } createTask(grpcOptions, key) { const { serviceName, options } = grpcOptions, nacosOptions = __rest(grpcOptions, ["serviceName", "options"]); const { package: packageName } = options, opt = __rest(options, ["package"]); if (!serviceName) throw new Error(`文件 grpc.options.ts 的 ${key} 对象缺少 serviceName 属性!`); const promise = (0, getNacosClientGrpc_1.getNacosClientGrpc)(Object.assign(Object.assign({ serviceName: serviceName, packageName }, nacosOptions), opt)); this.concurrentPool.push(promise); promise.then(res => { this.concurrentPool.splice(this.concurrentPool.indexOf(promise), 1); this.serviceGrpcMap.set(key, res); }).catch(err => { // todo 超时重试无效 this.grpcOptionsKeyArray.push(key); this.concurrentPool.splice(this.concurrentPool.indexOf(promise), 1); }); return promise; } getClientProvider(key) { if (this._grpcOptionsKeyArray.includes(key)) { const grpcOptions = this.grpcOptionsObject[key]; return { provide: key, useFactory: () => { return this.serviceGrpcMap.get(key); }, inject: [], }; } else { throw new Error(`文件 grpc.options.ts 不存在 ${key} 对象!`); } } } exports.ConnectionPool = ConnectionPool; //# sourceMappingURL=index.js.map