Skip to content

Commit

Permalink
feat(grpc): initialize default metadata
Browse files Browse the repository at this point in the history
- rpc.definition, eg. 'hero..HeroService'
- rpc.method, eg. 'findOne'
- rpc.method.type, unary|server|client
  • Loading branch information
waitingsong committed Nov 9, 2024
1 parent 860235f commit b5fe944
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
28 changes: 26 additions & 2 deletions packages/grpc/src/comsumer/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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,
Expand Down Expand Up @@ -71,6 +71,17 @@ export class GRPCClients extends Map {
connectionService[methodName] = (
clientOptions: IClientOptions = {}
) => {
if (clientOptions.metadata) {
const meta = new Metadata()

Check failure on line 75 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
meta.merge(clientOptions.metadata)

Check failure on line 76 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
clientOptions.metadata = meta

Check failure on line 77 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
}

Check failure on line 78 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Delete `⏎···········`
else {
clientOptions.metadata = new Metadata()

Check failure on line 80 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
}
clientOptions.metadata.set('rpc.definition', definition);
clientOptions.metadata.set('rpc.method', methodName);

return this.getClientRequestImpl(
connectionService,
originMethod,
Expand All @@ -89,35 +100,48 @@ 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;
}

Check failure on line 112 in packages/grpc/src/comsumer/clients.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Delete `⏎···`
else {
options.metadata = new Metadata();
}

let genericFunctionName;
switch (genericFunctionSelector) {
case 0:
options.metadata.set('rpc.method.type', 'unary');
genericFunctionName = new ClientUnaryRequest(
client,
originalFunction,
options
);
break;
case 1:
options.metadata.set('rpc.method.type', 'server'); // server streaming
genericFunctionName = new ClientReadableRequest(
client,
originalFunction,
options
);
break;
case 2:
options.metadata.set('rpc.method.type', 'client'); // client streaming
genericFunctionName = new ClientWritableRequest(
client,
originalFunction,
options
);
break;
case 3:
options.metadata.set('rpc.method.type', 'bidi'); // bidirectional streaming
genericFunctionName = new ClientDuplexStreamRequest(
client,
originalFunction,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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'
});
Expand Down

0 comments on commit b5fe944

Please sign in to comment.