Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting timeouts for grpc functions in node js

Tags:

node.js

grpc

Trying to create a timeout for grpc connection in the event that the server grpc implementation doesn't specify a callback function, however it seems that no matter what is specified in the options (new Date().getSeconds()+5) the client doesn't terminate the connection

    function hello (call, callback) {
        console.log(call.request.message)
    }
    server.addService(client.Hello.service, {hello: hello});
    server.bind('localhost:50051', grpc.ServerCredentials.createInsecure());
    server.start();
    grpcClient = new client.Hello('localhost:50051',
        grpc.credentials.createInsecure(),{deadline: new Date().getSeconds()+5}); //


    grpcClient.hello({message: "abc"}, function(err, response) {
        console.log(response) // doesn't reach here because function hello doesn't callback
    })
like image 477
Stanley Avatar asked Jun 02 '17 07:06

Stanley


People also ask

Does gRPC have a timeout?

Java Prime PackgRPC supports assigning timeouts to the requests. It is a way to perform cancellation of requests. It helps to avoid using the resources for both the client and the server for a request whose result would not be useful to the client.

What is default timeout for gRPC?

UNAVAILABLE "Connection timed out". Enabling gRPC client keepalives (setting gRPC KEEPALIVE_TIME) will cause the client to set TCP_USER_TIMEOUT to the same value as the gRPC KEEPALIVE_TIMEOUT (default: 20 seconds). The connection will then fail after 20 seconds with the same error: StatusCode.

What is the default timeout for Nodejs?

In Node. js, default server timeout is 0 milliseconds for the latest version and 2min i.e. (120000 milliseconds) for the older version.

What is server timeout Nodejs?

Definition and Usage. The server.timeout property sets, or gets, the server's timeout value, in milliseconds. Default is 2 minutes (120000 milliseconds) Set the server. timeout property to 0 to avoid having a default timeout value.


1 Answers

You can also set rpc deadline as:

function getRPCDeadline(rpcType) {

    timeAllowed = 5000
    switch(rpcType) {

        case 1:
            timeAllowed = 5000  // LIGHT RPC
            break

        case 2 :
            timeAllowed = 7000  // HEAVY RPC
            break

        default :
            console.log("Invalid RPC Type: Using Default Timeout")

    }

    return new Date( Date.now() + timeAllowed )

}

And then use this function while calling any rpc:

var deadline = getRPCDeadline(1)

grpcClient.hello({message: "abc"},{deadline: deadline}, function(err, response) {
    console.log(err)
    console.log(response)
});
like image 115
piyush pruthi Avatar answered Sep 28 '22 07:09

piyush pruthi