-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTlRpcClient.js
63 lines (53 loc) · 1.46 KB
/
TlRpcClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { credentials } = require('@grpc/grpc-js');
const TlRpc = require("./TlRpc");
/**
* rpc通用client
* @version 1.0.0
* @author iamtsm
*/
class TlRpcClient extends TlRpc{
/**
* -- 私有属性
* client实例
*/
static #clientInstance = new Map();
/**
* 初始化
* @param {*} rpcConfig
*/
constructor( rpcConfig ) {
super( rpcConfig )
}
/**
* 返回client缓存对象的key
*/
#clientInstanceKey() {
const config = this.getConfig();
return config.name + "-" + config.protoPackage + "-" + config.protoImpl + "-" + config.ip + "-" + config.port;
}
/**
* 获取client对象实例
* @returns
*/
clientInstance(){
const instance = this.implInstance();
if (!instance) {
return null;
}
const clientKey = this.#clientInstanceKey();
const client = TlRpcClient.#clientInstance.get(clientKey)
if (client) {
return client;
}
const config = this.getConfig();
const newClient = new instance(
config.ip + ":" + config.port,
credentials.createInsecure(),
{ 'grpc.service_config': '{"loadBalancingConfig": [{"round_robin": {}}]}' },
)
console.log("create client instance for " + clientKey + " ok!")
TlRpcClient.#clientInstance.set(clientKey, newClient);
return newClient;
}
}
module.exports = TlRpcClient;