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 437e92d commit 8412bae
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 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();
meta.merge(clientOptions.metadata);
clientOptions.metadata = meta;
}

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();
}
clientOptions.metadata.set('rpc.definition', definition);
clientOptions.metadata.set('rpc.method', methodName);

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

0 comments on commit 8412bae

Please sign in to comment.