@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.2
179 lines • 7.47 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.grpcNetworkManager = exports.UltipaClientAio = void 0;
const ultipa_grpc_pb_1 = require("../proto/ultipa_grpc_pb");
const grpc_js_1 = require("@grpc/grpc-js");
const ultipa_pb_1 = require("../proto/ultipa_pb");
const grpc = __importStar(require("@grpc/grpc-js"));
const testGrpc = false;
class UltipaClientAio {
rpcsClient;
controlsClient;
constructor(address, credentials, options) {
this.rpcsClient = new ultipa_grpc_pb_1.UltipaRpcsClient(address, credentials, options);
this.controlsClient = new ultipa_grpc_pb_1.UltipaControlsClient(address, credentials, options);
}
close() {
if (testGrpc) {
console.log("********** close");
}
this.rpcsClient.close();
this.controlsClient.close();
}
isUnavailable() {
let status = this.rpcsClient.getChannel().getConnectivityState(true);
let status2 = this.controlsClient.getChannel().getConnectivityState(true);
return status == grpc_js_1.connectivityState.SHUTDOWN || status2 == grpc_js_1.connectivityState.SHUTDOWN;
}
}
exports.UltipaClientAio = UltipaClientAio;
class GrpcNetworkManager {
clientCache;
timeIntervalMillionSeconds;
timeoutHandle;
log;
testClose = false;
constructor() {
this.clientCache = {};
this.timeIntervalMillionSeconds = 0;
}
keepConnectionAlive(timeIntervalSeconds) {
this.clearTimeoutHandle();
// timeIntervalSeconds = timeIntervalSeconds || 5
this.timeIntervalMillionSeconds = timeIntervalSeconds * 1000;
if (timeIntervalSeconds > 0) {
this.timeoutHandle = setInterval(() => {
if (this.log || testGrpc) {
console.log("grpc alive", new Date());
}
Object.keys(this.clientCache).forEach(key => {
if (key) {
try {
let client = this.clientCache[key];
if (client) {
if (this.testClose) {
client.close();
this.testClose = false;
}
let req = new ultipa_pb_1.HelloUltipaRequest();
req.setName("alive");
let sayHellDel = (err, res) => {
if (this.log) {
console.log({
key,
err_msg: err?.message,
res_msg: res?.getStatus().getMsg()
});
}
if (err) {
if (!testGrpc) {
console.log("**********: ", err);
}
this.removeUltipaRpcsClientWithKey(key);
}
};
client.rpcsClient.sayHello(req, sayHellDel);
client.controlsClient.sayHello(req, sayHellDel);
}
}
catch (error) {
if (this.log) {
console.log(error);
}
this.removeUltipaRpcsClientWithKey(key);
}
}
});
}, this.timeIntervalMillionSeconds);
}
}
removeConnectionAlive() {
this.clearTimeoutHandle();
}
clearTimeoutHandle() {
if (this.timeoutHandle) {
clearInterval(this.timeoutHandle);
this.timeoutHandle = null;
}
}
_cacheKey(host, crt) {
let crtString = crt ? crt.toString() : "";
let cacheKey = host + crtString;
return cacheKey;
}
removeUltipaRpcsClient(host, crt) {
let key = this._cacheKey(host, crt);
this.removeUltipaRpcsClientWithKey(key);
}
removeUltipaRpcsClientWithKey(key) {
this.clientCache[key]?.close();
this.clientCache[key] = null;
}
isHostDomain(host) {
const hostname = host.split(':')[0];
return hostname.includes('.') && !/^\d+\.\d+\.\d+\.\d+$/.test(hostname);
}
getUltipaRpcsClient(host, crt) {
let cacheKey = this._cacheKey(host, crt);
if (this.clientCache[cacheKey]) {
try {
if (this.clientCache[cacheKey].isUnavailable()) {
this.removeUltipaRpcsClientWithKey(cacheKey);
}
}
catch (err) {
this.removeUltipaRpcsClientWithKey(cacheKey);
}
}
if (!this.clientCache[cacheKey]) {
const baseOpt = {
"grpc.max_send_message_length": -1,
"grpc.max_receive_message_length": -1,
"grpc.keepalive_timeout_ms": 1500,
"grpc.enable_retries": 1,
};
const isDomain = this.isHostDomain(host);
if (crt) {
const ssl_cred = grpc.credentials.createSsl(crt);
this.clientCache[cacheKey] = new UltipaClientAio(host, ssl_cred, {
"grpc.ssl_target_name_override": "ultipa",
"grpc.default_authority": "ultipa",
...baseOpt,
});
}
else if (isDomain) {
const ssl_cred = grpc.credentials.createSsl();
this.clientCache[cacheKey] = new UltipaClientAio(host, ssl_cred, baseOpt);
}
else {
this.clientCache[cacheKey] = new UltipaClientAio(host, grpc.credentials.createInsecure(), baseOpt);
}
}
return this.clientCache[cacheKey];
}
}
exports.grpcNetworkManager = new GrpcNetworkManager();
//# sourceMappingURL=network.manager.js.map