@@ -30,22 +30,21 @@ import { type TerminateWorkflowExecutionRequest__Input } from '@/__generated__/p
30
30
import { type TerminateWorkflowExecutionResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/TerminateWorkflowExecutionResponse' ;
31
31
import { type UpdateDomainRequest__Input } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainRequest' ;
32
32
import { type UpdateDomainResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainResponse' ;
33
+ import { type ClusterConfig } from '@/config/dynamic/resolvers/clusters.types' ;
33
34
34
35
import grpcServiceConfigurations from '../../config/grpc/grpc-services-config' ;
35
36
import getConfigValue from '../config/get-config-value' ;
37
+ import GlobalRef from '../global-ref' ;
36
38
37
39
import GRPCService , {
38
40
type GRPCMetadata ,
39
41
type GRPCRequestConfig ,
40
42
} 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
48
46
> ;
47
+ type ClustersServices = Record < string , ClusterService > ;
49
48
export type GRPCClusterMethods = {
50
49
archivedWorkflows : (
51
50
payload : ListArchivedWorkflowExecutionsRequest__Input
@@ -100,50 +99,70 @@ export type GRPCClusterMethods = {
100
99
) => Promise < RequestCancelWorkflowExecutionResponse > ;
101
100
} ;
102
101
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
+ } ;
110
118
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
+ } ) ;
131
139
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 ;
140
159
} ;
141
160
142
161
const getClusterServicesMethods = async (
143
162
c : string ,
144
163
metadata ?: GRPCMetadata
145
164
) : Promise < GRPCClusterMethods > => {
146
- const clusterServices = await getClusterServices ( ) ;
165
+ const clusterServices = await getAllClustersServices ( ) ;
147
166
const { visibilityService, adminService, domainService, workflowService } =
148
167
clusterServices [ c ] ;
149
168
0 commit comments