1

Grpc-node: How to edit metadata and send back edited metadata to client?

Below is currently what I have at the moment, and it seems like the metadata can be passed to the grpc-client RPC method ('greetmath'), but I can't edit the metadata in the server and send it back to the client. It can only send back metadata that was originally created.

Is there anyway I can edit the metadata in the grpc server and send that edited metadata to the grpc client?

Greeter_server.js

const path = require('path');
const PROTO_PATH = path.join(__dirname, '../proto/greet.proto');
// console.log("Proto path: ", PROTO_PATH);
const protoLoader = require('@grpc/proto-loader') //require('@grpc/proto-loader')
const grpc = require('grpc')


//grpc service definition for greet
const greetProtoDefinition = protoLoader.loadSync(PROTO_PATH, {
     keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true
});

const greetPackageDefinition = grpc.loadPackageDefinition(greetProtoDefinition).greet

function greetFunc(call, callback) {
    var firstName = call.request.greeting.first_name;
    var lastName = call.request.greeting.last_name;

    callback(null, {result: "Hello " + firstName + " " + lastName});
}


function greetMath(call, callback) {
    console.log("callback: ", call);
    console.log("Meta data: ", call.metadata._internal_repr.somekey);
    call.metadata._internal_repr.somekey.push('random');    

    var firstName = call.request.greeting.first_name;
    var lastName = call.request.greeting.last_name;
    let current = Number(process.hrtime.bigint());
    console.log("call obj: ", call);
    console.log("callback obj: ", callback);
    callback(null, {result: "Hello " + firstName + " " + lastName + " current: " + current});

}

function main() {
    const server = new grpc.Server()

    server.addService(greetPackageDefinition.GreetService.service, {
         greet: greetFunc,
         greetMath: greetMath
    });

    server.bind("127.0.0.1:4000", grpc.ServerCredentials.createInsecure());
    server.start();
    console.log("Server Running at http://127.0.0.1:50051")
}
main()

greet.proto

syntax = "proto3";

package greet;

service GreetService {
    //Unary API
    rpc Greet (GreetRequest) returns (GreetResponse) {};
    rpc GreetMath(GreetRequest) returns (GreetResponse) {};
}

message Greeting {
    string first_name = 1;
    string last_name = 2;
}

message GreetRequest {
     Greeting greeting = 1;
}

message GreetResponse {
     string result = 1;
}

greeter_client.js

const path = require('path');
const PROTO_PATH = path.join(__dirname, '../proto/greet.proto');
const protoLoader = require('@grpc/proto-loader') //require('@grpc/proto-loader')
const grpc = require('grpc')


//grpc service definition for greet
const greetProtoDefinition = protoLoader.loadSync(PROTO_PATH, {
     keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true
});

const greetPackageDefinition = grpc.loadPackageDefinition(greetProtoDefinition).greet

const client = new greetPackageDefinition.GreetService("localhost:4000",
        grpc.credentials.createInsecure()
)


function callGreetings() {
    var request = {
         greeting: {
              first_name: "Jerry",
              last_name: "Tom"
         }
    }
     
    client.greet(request, (error, response) => {
        if(!error) {
            console.log("Greeting Response: ", response.result);
        } else {
            console.error(error)
        }
    })
}


function callGreetingsLogger() {
    var request = {
        greeting: {
             first_name: "Jerry",
             last_name: "Tom"
        }
   }
   let meta = new grpc.Metadata();
   meta.add('someKey', 'someVal'); 
   let end;
   let start = Number(process.hrtime.bigint());

   client.greetMath(request, meta, (error, response) => {
        if(!error) {
           console.log("res: ", response);
           console.log("metadata: ", meta);
           console.log("Greeting Response: ", response.result);
           end = Number(process.hrtime.bigint());
    

           console.log("start: ", start);
           console.log("end: ", end);
           console.log("end - start", (end - start)/ 1000000, "ms");
           
        function getDateTime() {
            var date = new Date();
            var hour = date.getHours();
            hour = (hour < 10 ? "0" : "") + hour;
            var min  = date.getMinutes();
            min = (min < 10 ? "0" : "") + min;
            var sec  = date.getSeconds(); sec = (sec < 10 ? "0" : "") + sec; 
            var year = date.getFullYear();
            var month = date.getMonth() + 1; month = (month < 10 ? "0" : "") + month;
            var day  = date.getDate(); day = (day < 10 ? "0" : "") + day;
            return month + "/" + day + "/" + year + "  |  Time: " + hour + ":" + min + ":" + sec;
        }

        let currentTime = getDateTime();
           console.log("Current time: ", currentTime);
       
       } else {
           console.error(error)
       }
   });

}

function main() {
    callGreetings();
    callGreetingsLogger();
}

main()

4

2 回答 2

1

问题是“我无法编辑服务器中的元数据并将其发送回客户端”。问题中的代码编辑元数据,所以我假设这是您尝试使用的代码,但这不是发送元数据,因为它从不sendMetadata调用call. 您需要调用该方法将元数据发送回客户端。

此外,您不应该触摸_internal_repr. metadata客户端代码通过调用meta.add(). 服务器代码也应该这样做。所以,而不是call.metadata._internal_repr.somekey你应该写call.metadata.get('somekey'),而不是call.metadata._internal_repr.somekey.push('random')你应该写call.metadata.add('somekey', 'random')

于 2020-07-22T17:06:19.167 回答
0

我怀疑您正在尝试更改 gRPC 的内部元数据 ( call.metadata._internal_repr),这是不允许的。

例如,在 Go(lang) 中,为 Metadata 公开的唯一方法是 egGetMetadata()并且故意没有公共机制来更改此数据。

你有一些选择:

  • 创建您在消息中携带的元数据消息类型。
  • 如果您尝试扩展 gRPC 的功能,请考虑自定义选项扩展。
于 2020-07-21T17:54:06.387 回答