@ultipa-graph/ultipa-node-sdk
Version:
NodeJS SDK for ultipa-server 4.0
119 lines • 5.25 kB
JavaScript
"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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionPool = void 0;
const bluebird_1 = __importDefault(require("bluebird"));
const connection_final_1 = require("./connection.final");
/** ultipa 连接池 */
class ConnectionPool {
constructor(hosts, username, password, crt, defaultConfig, otherParams) {
this.hosts = hosts;
this.username = username;
this.password = password;
this.crt = crt;
this.defaultConfig = defaultConfig;
this.otherParams = otherParams;
// this.clients = []
// for (const host of this.hosts) {
// let client = new ConnectionFinal(host, this.username, this.password, this.crt, this.defaultConfig)
// this.clients.push(client)
// }
this.currentIndex = 0;
this.initCrt();
}
initCrt() {
var _a;
if (((_a = this.otherParams) === null || _a === void 0 ? void 0 : _a.isHttps) && !this.crt) {
this.crt = Buffer.from("https");
}
}
/** 获得有效连接 */
getActive(hosts) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
hosts = hosts || this.hosts;
let client = yield this._getActive(hosts);
if (!((_a = this.otherParams) === null || _a === void 0 ? void 0 : _a.isHttp) && !client.testConnectResult && !this.crt) {
try {
// https
let client_https = yield this._getActive(hosts, Buffer.from("https"));
console.log("try https:", hosts);
if (client_https.testConnectResult) {
return client_https;
}
}
catch (error) {
// 忽略
console.log(error);
}
}
return client;
});
}
_getActive(hosts, crt) {
return __awaiter(this, void 0, void 0, function* () {
let client = yield new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
let clientOne = null;
yield bluebird_1.default.map(hosts, (host, i) => __awaiter(this, void 0, void 0, function* () {
let client = new connection_final_1.ConnectionFinal(host, this.username, this.password, crt || this.crt, this.defaultConfig);
clientOne = client;
// console.log(i, host)
let test = yield client.test({
useHost: host,
});
client.testConnectResult = test;
// console.log(i, host, test)
if (test) {
resolve(client);
}
}));
resolve(clientOne);
}));
return client;
});
}
_testActiveMaybeSlow(noBalanced = false) {
return __awaiter(this, void 0, void 0, function* () {
let m = noBalanced ? 0 : 1;
let client = null;
for (let index = 0; index < this.hosts.length; index++) {
let i = (index + this.currentIndex + m) % this.hosts.length;
let host = this.hosts[i];
client = new connection_final_1.ConnectionFinal(host, this.username, this.password, this.crt, this.defaultConfig);
let test = yield client.test({
useHost: host
});
if (test) {
this.currentIndex = i;
return client;
}
}
return client;
});
}
test(requestConfig) {
return __awaiter(this, void 0, void 0, function* () {
let v = {};
yield bluebird_1.default.map(this.hosts, (host) => __awaiter(this, void 0, void 0, function* () {
let client = yield this.getActive([host]);
// let client = new ConnectionFinal(host, this.username, this.password, this.crt, this.defaultConfig)
let test = yield client.test_has_message(Object.assign(Object.assign({}, requestConfig), { useHost: host }));
v[host] = test;
}));
return v;
});
}
}
exports.ConnectionPool = ConnectionPool;
//# sourceMappingURL=connection.pool.js.map