diff --git a/packages/grpc/src/comsumer/clients.ts b/packages/grpc/src/comsumer/clients.ts index e8f03a5b19bc..9712be684627 100644 --- a/packages/grpc/src/comsumer/clients.ts +++ b/packages/grpc/src/comsumer/clients.ts @@ -8,7 +8,7 @@ import { Utils, ILogger, } from '@midwayjs/core'; -import { credentials, loadPackageDefinition } from '@grpc/grpc-js'; +import { credentials, loadPackageDefinition, Metadata } from '@grpc/grpc-js'; import { DefaultConfig, IClientOptions, @@ -60,6 +60,17 @@ export class GRPCClients extends Map { connectionService[methodName] = ( clientOptions: IClientOptions = {} ) => { + if (clientOptions.metadata) { + const meta = new Metadata() + meta.merge(clientOptions.metadata) + clientOptions.metadata = meta + } + else { + clientOptions.metadata = new Metadata() + } + clientOptions.metadata.set('rpc.definition', definition); + clientOptions.metadata.set('rpc.method', methodName); + return this.getClientRequestImpl( connectionService, originMethod, @@ -79,14 +90,24 @@ export class GRPCClients extends Map { return this.get(serviceName); } - getClientRequestImpl(client, originalFunction, options = {}) { + getClientRequestImpl(client, originalFunction, options: IClientOptions = {}) { const genericFunctionSelector = (originalFunction.requestStream ? 2 : 0) | (originalFunction.responseStream ? 1 : 0); + if (options.metadata) { + const meta = new Metadata(); + meta.merge(options.metadata); + options.metadata = meta; + } + else { + options.metadata = new Metadata(); + } + let genericFunctionName; switch (genericFunctionSelector) { case 0: + options.metadata.set('rpc.method.type', 'unary'); genericFunctionName = new ClientUnaryRequest( client, originalFunction, @@ -94,6 +115,7 @@ export class GRPCClients extends Map { ); break; case 1: + options.metadata.set('rpc.method.type', 'server'); // server streaming genericFunctionName = new ClientReadableRequest( client, originalFunction, @@ -101,6 +123,7 @@ export class GRPCClients extends Map { ); break; case 2: + options.metadata.set('rpc.method.type', 'client'); // client streaming genericFunctionName = new ClientWritableRequest( client, originalFunction, @@ -108,6 +131,7 @@ export class GRPCClients extends Map { ); break; case 3: + options.metadata.set('rpc.method.type', 'bidi'); // bidirectional streaming genericFunctionName = new ClientDuplexStreamRequest( client, originalFunction, diff --git a/packages/grpc/test/fixtures/base-app-multiple-service/src/provider/hero.ts b/packages/grpc/test/fixtures/base-app-multiple-service/src/provider/hero.ts index a286464fc99b..dba390826353 100644 --- a/packages/grpc/test/fixtures/base-app-multiple-service/src/provider/hero.ts +++ b/packages/grpc/test/fixtures/base-app-multiple-service/src/provider/hero.ts @@ -1,11 +1,15 @@ +import * as assert from 'assert'; import { GrpcMethod, MSProviderType, Provider, Provide, Inject, Init } from '@midwayjs/core'; import { helloworld, hero } from '../interface'; -import { Clients } from '../../../../../src'; +import { Clients, Context } from '../../../../../src'; @Provide() @Provider(MSProviderType.GRPC, { package: 'hero' }) export class HeroService implements hero.HeroService { + @Inject() + ctx: Context; + @Inject() grpcClients: Clients; @@ -18,6 +22,19 @@ export class HeroService implements hero.HeroService { @GrpcMethod() async findOne(data) { + assert(this.ctx, 'should get context'); + const { metadata } = this.ctx; + assert(metadata, 'should get metadata'); + + const rpcDefinition = metadata.get('rpc.definition'); + assert(rpcDefinition[0] === 'hero.HeroService', 'should get rpc.definition'); + + const rpcMethod = metadata.get('rpc.method'); + assert(rpcMethod[0] === 'findOne', 'should get rpc.method'); + + const rpcMethodType = metadata.get('rpc.method.type'); + assert(rpcMethodType[0] === 'unary', 'should get rpc.method.type'); + const result = await this.greeterService.sayHello().sendMessage({ name: 'harry' });