Skip to content

Commit 61459cb

Browse files
author
Alex Dukhnovskiy
committed
fix(microcervices): fix multipackages client
1 parent ebfee28 commit 61459cb

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

packages/microservices/client/client-grpc.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
2222
protected readonly logger = new Logger(ClientProxy.name);
2323
protected readonly clients = new Map<string, any>();
2424
protected readonly url: string;
25-
protected grpcClient: any;
25+
protected grpcClients: any[] = [];
2626

2727
constructor(protected readonly options: GrpcOptions['options']) {
2828
super();
@@ -35,12 +35,18 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
3535
require('grpc'),
3636
);
3737
grpcProtoLoaderPackage = loadPackage(protoLoader, ClientGrpcProxy.name);
38-
this.grpcClient = this.createClient();
38+
this.grpcClients = this.createClients();
3939
}
4040

4141
public getService<T extends {}>(name: string): T {
4242
const grpcClient = this.createClientByServiceName(name);
43-
const protoMethods = Object.keys(this.grpcClient[name].prototype);
43+
const getClient = this.getClient(name);
44+
45+
if (!getClient) {
46+
throw new InvalidGrpcServiceException();
47+
}
48+
49+
const protoMethods = Object.keys(getClient[name].prototype);
4450
const grpcService = {} as T;
4551

4652
protoMethods.forEach(m => {
@@ -55,7 +61,9 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
5561
}
5662

5763
public createClientByServiceName(name: string) {
58-
if (!this.grpcClient[name]) {
64+
const getClient = this.getClient(name);
65+
66+
if (!getClient) {
5967
throw new InvalidGrpcServiceException();
6068
}
6169
const maxSendMessageLengthKey = 'grpc.max_send_message_length';
@@ -86,11 +94,7 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
8694
options.credentials || grpcPackage.credentials.createInsecure();
8795

8896
delete options.credentials;
89-
const grpcClient = new this.grpcClient[name](
90-
this.url,
91-
credentials,
92-
options,
93-
);
97+
const grpcClient = new getClient[name](this.url, credentials, options);
9498
this.clients.set(name, grpcClient);
9599
return grpcClient;
96100
}
@@ -155,23 +159,28 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
155159
};
156160
}
157161

158-
public createClient(): any {
162+
public createClients(): any[] {
159163
const grpcContext = this.loadProto();
160164
const packageOpt = this.getOptionsProp(this.options, 'package');
161-
165+
const grpcPkgs = [];
162166
const packageNames = Array.isArray(packageOpt) ? packageOpt : [packageOpt];
163167

164168
for (const packageName of packageNames) {
165169
const grpcPkg = this.lookupPackage(grpcContext, packageName);
166170

167171
if (!grpcPkg) {
168172
const invalidPackageError = new InvalidGrpcPackageException();
169-
this.logger.error(invalidPackageError.message, invalidPackageError.stack);
173+
this.logger.error(
174+
invalidPackageError.message,
175+
invalidPackageError.stack,
176+
);
170177
throw invalidPackageError;
171178
}
172179

173-
return grpcPkg;
180+
grpcPkgs.push(grpcPkg);
174181
}
182+
183+
return grpcPkgs;
175184
}
176185

177186
public loadProto(): any {
@@ -204,8 +213,11 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
204213
}
205214

206215
public close() {
207-
this.grpcClient && this.grpcClient.close();
208-
this.grpcClient = null;
216+
this.grpcClients.forEach(client => {
217+
client.close();
218+
});
219+
220+
this.grpcClients = [];
209221
}
210222

211223
public async connect(): Promise<any> {
@@ -221,6 +233,10 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
221233
);
222234
}
223235

236+
protected getClient(name: string): any {
237+
return this.grpcClients.find(client => client.hasOwnProperty(name));
238+
}
239+
224240
protected publish(packet: any, callback: (packet: any) => any): any {
225241
throw new Error(
226242
'Method is not supported in gRPC mode. Use ClientGrpc instead (learn more in the documentation).',

packages/microservices/test/client/client-grpc.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('ClientGrpcProxy', () => {
3333
protoPath: ['test.proto', 'test2.proto'],
3434
package: ['test', 'test2'],
3535
loader: {
36-
includeDirs: [join(__dirname, '.')]
37-
}
36+
includeDirs: [join(__dirname, '.')],
37+
},
3838
});
3939
});
4040

@@ -225,14 +225,14 @@ describe('ClientGrpcProxy', () => {
225225
});
226226
});
227227

228-
describe('createClient', () => {
228+
describe('createClients', () => {
229229
describe('when package does not exist', () => {
230230
it('should throw "InvalidGrpcPackageException"', () => {
231231
sinon.stub(client, 'lookupPackage').callsFake(() => null);
232232
(client as any).logger = new NoopLogger();
233233

234234
try {
235-
client.createClient();
235+
client.createClients();
236236
} catch (err) {
237237
expect(err).to.be.instanceof(InvalidGrpcPackageException);
238238
}

0 commit comments

Comments
 (0)