-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
grpc-client.ts
66 lines (56 loc) · 2.36 KB
/
grpc-client.ts
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
64
65
66
import { IClientConfig } from './interfaces/grpc-configuration.interface';
import { ClientGrpcProxy } from '@nestjs/microservices/client';
import { NestCloud } from '@nestcloud/core';
import { ILoadbalance, IServer } from '@nestcloud/common';
import { GrpcDelegate } from '@nestcloud/loadbalance';
export class GrpcClient {
private readonly config: IClientConfig;
private readonly proxy: ClientGrpcProxy;
private readonly proxyCache = new Map<string, ClientGrpcProxy>();
private readonly serviceCache = new Map<string, any>();
constructor(config: IClientConfig) {
this.config = config;
this.proxy = new ClientGrpcProxy(config);
}
public getService<T extends {}>(name: string): T {
const noClusterService = this.proxy.getService<T>(name);
const grpcService = {} as T;
const protoMethods = Object.keys(noClusterService);
protoMethods.forEach(key => {
grpcService[key] = (...args: any[]) => {
let { service, node } = this.getProxyService<T>(name, key);
if (!service) {
service = noClusterService;
return service[key](...args);
}
return new GrpcDelegate(node, service).execute(key, ...args);
};
});
return grpcService;
}
private scheduleCleanCache() {
}
private getProxyService<T extends {}>(name: string, method: string): { service: T, node: IServer } {
const lb: ILoadbalance = NestCloud.global.loadbalance;
if (!lb) {
return { service: null, node: null };
}
const node = lb.choose(this.config.service);
const methodKey = `${node.id}/${method}`;
if (!this.serviceCache.get(methodKey)) {
if (!this.proxyCache.has(node.id)) {
const proxy = new ClientGrpcProxy({
url: `${node.address}:${node.port}`,
package: this.config.package,
protoPath: this.config.protoPath,
});
this.proxyCache.set(node.id, proxy);
}
const proxy = this.proxyCache.get(node.id);
const service = proxy.getService<T>(name);
this.serviceCache.set(methodKey, service);
}
const service = this.serviceCache.get(methodKey) as T;
return { service, node };
}
}