manikawnth
manikawnth

Reputation: 3229

How to set timeout of grpc client in node.js

I'm referring to the following example of node-grpc client: https://github.com/grpc/grpc/blob/master/examples/node/dynamic_codegen/greeter_client.js

//create a client
var client = new hello_proto.Greeter('localhost:50051',
                                       grpc.credentials.createInsecure());

//issue the call
  client.sayHello({name: user}, function(err, response) {
    console.log('Greeting:', response.message);
  });

In this call format, where do I provide the call deadline options.

Also, the jsdoc at https://grpc.io/grpc/node/ never has this kind of API calls. Is there a good tutorial on this which covers examples like streaming rpcs, timeouts, securing the channels etc?

Upvotes: 1

Views: 3285

Answers (1)

murgatroid99
murgatroid99

Reputation: 20277

There's an optional argument to pass additional options between the request argument and the callback. This includes a deadline key. So you would do something like this:

client.sayHello({name: user}, {deadline: deadline}, function(err, response) {
  console.log('Greeting:', response.message);
});

Deadline can either be a date object or Infinity to explicitly not have the call time out.

This is documented, sort of, as the Client#makeUnaryRequest function; just ignore the first three arguments. That mentions the optional options argument, and its type Client~CallOptions describes all of the options that can be passed there.

Upvotes: 2

Related Questions