Skip to content

Commit b984fdd

Browse files
Assem-HafezAssem-Uber
authored andcommitted
cache grpc services (#799)
* cache grpc serives * remove logs * fix typo
1 parent 83657cd commit b984fdd

File tree

4 files changed

+72
-45
lines changed

4 files changed

+72
-45
lines changed

src/utils/config/__tests__/global-ref.node.ts renamed to src/utils/__tests__/global-ref.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ describe('GlobalRef', () => {
2323
expect(globalRef.value).toBeUndefined();
2424
});
2525

26+
it('should return default value if value is not set and default value is passed', () => {
27+
const globalRef = new GlobalRef<number>('unique-name', 100);
28+
expect(globalRef.value).toBe(100);
29+
});
30+
2631
it('should handle different types of values', () => {
2732
const stringRef = new GlobalRef<string>('string-unique-name');
2833
stringRef.value = 'test string';

src/utils/config/global-configs-ref.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import GlobalRef from '../global-ref';
2+
13
import { type LoadedConfigs } from './config.types';
2-
import GlobalRef from './global-ref';
34

45
const globalConfigRef = new GlobalRef<LoadedConfigs>('cadence-config');
56
const setLoadedGlobalConfigs = (c: LoadedConfigs): void => {

src/utils/config/global-ref.ts renamed to src/utils/global-ref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export default class GlobalRef<T> {
22
private readonly sym: symbol;
33

4-
constructor(uniqueName: string) {
4+
constructor(uniqueName: string, defaultValue?: T) {
55
this.sym = Symbol.for(uniqueName);
6+
const g = global as any;
7+
if (g[this.sym] === undefined) g[this.sym] = defaultValue;
68
}
79

810
get value() {

src/utils/grpc/grpc-client.ts

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,21 @@ import { type TerminateWorkflowExecutionRequest__Input } from '@/__generated__/p
3030
import { type TerminateWorkflowExecutionResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/TerminateWorkflowExecutionResponse';
3131
import { type UpdateDomainRequest__Input } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainRequest';
3232
import { type UpdateDomainResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainResponse';
33+
import { type ClusterConfig } from '@/config/dynamic/resolvers/clusters.types';
3334

3435
import grpcServiceConfigurations from '../../config/grpc/grpc-services-config';
3536
import getConfigValue from '../config/get-config-value';
37+
import GlobalRef from '../global-ref';
3638

3739
import GRPCService, {
3840
type GRPCMetadata,
3941
type GRPCRequestConfig,
4042
} from './grpc-service';
41-
42-
type ClusterServices = Record<
43-
string,
44-
Record<
45-
'adminService' | 'domainService' | 'visibilityService' | 'workflowService',
46-
GRPCService
47-
>
43+
type ClusterService = Record<
44+
'adminService' | 'domainService' | 'visibilityService' | 'workflowService',
45+
GRPCService
4846
>;
47+
type ClustersServices = Record<string, ClusterService>;
4948
export type GRPCClusterMethods = {
5049
archivedWorkflows: (
5150
payload: ListArchivedWorkflowExecutionsRequest__Input
@@ -100,50 +99,70 @@ export type GRPCClusterMethods = {
10099
) => Promise<RequestCancelWorkflowExecutionResponse>;
101100
};
102101

103-
const getClusterServices = async () => {
104-
const CLUSTERS_CONFIGS = await getConfigValue('CLUSTERS');
105-
return CLUSTERS_CONFIGS.reduce((result, c) => {
106-
const requestConfig: GRPCRequestConfig = {
107-
serviceName: c.grpc.serviceName,
108-
metadata: c.grpc.metadata,
109-
};
102+
// cache services instances
103+
const clusterServicesMapGlobalRef = new GlobalRef<ClustersServices>(
104+
'cluster-services-map',
105+
{}
106+
);
107+
const clusterServicesMap: ClustersServices = clusterServicesMapGlobalRef.value;
108+
109+
const getClusterServices = async (c: ClusterConfig) => {
110+
if (clusterServicesMap[c.clusterName]) {
111+
return clusterServicesMap[c.clusterName];
112+
}
113+
114+
const requestConfig: GRPCRequestConfig = {
115+
serviceName: c.grpc.serviceName,
116+
metadata: c.grpc.metadata,
117+
};
110118

111-
const adminService = new GRPCService({
112-
peer: c.grpc.peer,
113-
requestConfig,
114-
...grpcServiceConfigurations.adminServiceConfig,
115-
});
116-
const domainService = new GRPCService({
117-
peer: c.grpc.peer,
118-
requestConfig,
119-
...grpcServiceConfigurations.domainServiceConfig,
120-
});
121-
const visibilityService = new GRPCService({
122-
peer: c.grpc.peer,
123-
requestConfig,
124-
...grpcServiceConfigurations.visibilityServiceConfig,
125-
});
126-
const workflowService = new GRPCService({
127-
peer: c.grpc.peer,
128-
requestConfig,
129-
...grpcServiceConfigurations.workflowServiceConfig,
130-
});
119+
const adminService = new GRPCService({
120+
peer: c.grpc.peer,
121+
requestConfig,
122+
...grpcServiceConfigurations.adminServiceConfig,
123+
});
124+
const domainService = new GRPCService({
125+
peer: c.grpc.peer,
126+
requestConfig,
127+
...grpcServiceConfigurations.domainServiceConfig,
128+
});
129+
const visibilityService = new GRPCService({
130+
peer: c.grpc.peer,
131+
requestConfig,
132+
...grpcServiceConfigurations.visibilityServiceConfig,
133+
});
134+
const workflowService = new GRPCService({
135+
peer: c.grpc.peer,
136+
requestConfig,
137+
...grpcServiceConfigurations.workflowServiceConfig,
138+
});
131139

132-
result[c.clusterName] = {
133-
adminService,
134-
domainService,
135-
visibilityService,
136-
workflowService,
137-
};
138-
return result;
139-
}, {} as ClusterServices);
140+
const services: ClusterService = {
141+
adminService,
142+
domainService,
143+
visibilityService,
144+
workflowService,
145+
};
146+
// add service to cache (clusterServicesMap)
147+
clusterServicesMap[c.clusterName] = services;
148+
return services;
149+
};
150+
151+
const getAllClustersServices = async () => {
152+
const CLUSTERS_CONFIGS = await getConfigValue('CLUSTERS');
153+
const clustersServices: ClustersServices = {};
154+
155+
for (const c of CLUSTERS_CONFIGS) {
156+
clustersServices[c.clusterName] = await getClusterServices(c);
157+
}
158+
return clustersServices;
140159
};
141160

142161
const getClusterServicesMethods = async (
143162
c: string,
144163
metadata?: GRPCMetadata
145164
): Promise<GRPCClusterMethods> => {
146-
const clusterServices = await getClusterServices();
165+
const clusterServices = await getAllClustersServices();
147166
const { visibilityService, adminService, domainService, workflowService } =
148167
clusterServices[c];
149168

0 commit comments

Comments
 (0)