Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache grpc services #799

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('GlobalRef', () => {
expect(globalRef.value).toBeUndefined();
});

it('should return default value if value is not set and default value is passed', () => {
const globalRef = new GlobalRef<number>('unique-name', 100);
expect(globalRef.value).toBe(100);
});

it('should handle different types of values', () => {
const stringRef = new GlobalRef<string>('string-unique-name');
stringRef.value = 'test string';
Expand Down
3 changes: 2 additions & 1 deletion src/utils/config/global-configs-ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import GlobalRef from '../global-ref';

import { type LoadedConfigs } from './config.types';
import GlobalRef from './global-ref';

const globalConfigRef = new GlobalRef<LoadedConfigs>('cadence-config');
const setLoadedGlobalConfigs = (c: LoadedConfigs): void => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/config/global-ref.ts → src/utils/global-ref.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export default class GlobalRef<T> {
private readonly sym: symbol;

constructor(uniqueName: string) {
constructor(uniqueName: string, defaultValue?: T) {
this.sym = Symbol.for(uniqueName);
const g = global as any;
if (g[this.sym] === undefined) g[this.sym] = defaultValue;
}

get value() {
Expand Down
105 changes: 62 additions & 43 deletions src/utils/grpc/grpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@ import { type TerminateWorkflowExecutionRequest__Input } from '@/__generated__/p
import { type TerminateWorkflowExecutionResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/TerminateWorkflowExecutionResponse';
import { type UpdateDomainRequest__Input } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainRequest';
import { type UpdateDomainResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/UpdateDomainResponse';
import { type ClusterConfig } from '@/config/dynamic/resolvers/clusters.types';

import grpcServiceConfigurations from '../../config/grpc/grpc-services-config';
import getConfigValue from '../config/get-config-value';
import GlobalRef from '../global-ref';

import GRPCService, {
type GRPCMetadata,
type GRPCRequestConfig,
} from './grpc-service';

type ClusterServices = Record<
string,
Record<
'adminService' | 'domainService' | 'visibilityService' | 'workflowService',
GRPCService
>
type ClusterService = Record<
'adminService' | 'domainService' | 'visibilityService' | 'workflowService',
GRPCService
>;
type ClustersServices = Record<string, ClusterService>;
export type GRPCClusterMethods = {
archivedWorkflows: (
payload: ListArchivedWorkflowExecutionsRequest__Input
Expand Down Expand Up @@ -100,50 +99,70 @@ export type GRPCClusterMethods = {
) => Promise<RequestCancelWorkflowExecutionResponse>;
};

const getClusterServices = async () => {
const CLUSTERS_CONFIGS = await getConfigValue('CLUSTERS');
return CLUSTERS_CONFIGS.reduce((result, c) => {
const requestConfig: GRPCRequestConfig = {
serviceName: c.grpc.serviceName,
metadata: c.grpc.metadata,
};
// cache services instances
const clusterServicesMapGlobalRef = new GlobalRef<ClustersServices>(
'cluster-services-map',
{}
);
const clusterServicesMap: ClustersServices = clusterServicesMapGlobalRef.value;

const getClusterServices = async (c: ClusterConfig) => {
if (clusterServicesMap[c.clusterName]) {
return clusterServicesMap[c.clusterName];
}

const requestConfig: GRPCRequestConfig = {
serviceName: c.grpc.serviceName,
metadata: c.grpc.metadata,
};

const adminService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.adminServiceConfig,
});
const domainService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.domainServiceConfig,
});
const visibilityService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.visibilityServiceConfig,
});
const workflowService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.workflowServiceConfig,
});
const adminService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.adminServiceConfig,
});
const domainService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.domainServiceConfig,
});
const visibilityService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.visibilityServiceConfig,
});
const workflowService = new GRPCService({
peer: c.grpc.peer,
requestConfig,
...grpcServiceConfigurations.workflowServiceConfig,
});

result[c.clusterName] = {
adminService,
domainService,
visibilityService,
workflowService,
};
return result;
}, {} as ClusterServices);
const services: ClusterService = {
adminService,
domainService,
visibilityService,
workflowService,
};
// add service to cache (clusterServicesMap)
clusterServicesMap[c.clusterName] = services;
return services;
};

const getAllClustersServices = async () => {
const CLUSTERS_CONFIGS = await getConfigValue('CLUSTERS');
const clustersServices: ClustersServices = {};

for (const c of CLUSTERS_CONFIGS) {
clustersServices[c.clusterName] = await getClusterServices(c);
}
return clustersServices;
};

const getClusterServicesMethods = async (
c: string,
metadata?: GRPCMetadata
): Promise<GRPCClusterMethods> => {
const clusterServices = await getClusterServices();
const clusterServices = await getAllClustersServices();
const { visibilityService, adminService, domainService, workflowService } =
clusterServices[c];

Expand Down