在节点js中设置grpc函数的超时

时间:2017-06-02 07:55:12

标签: node.js grpc

如果服务器grpc实现没有指定回调函数,请尝试为grpc连接创建超时,但无论选项中指定了什么(new date()。getSeconds()+ 5 )客户端不终止连接

    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
    })

3 个答案:

答案 0 :(得分:3)

您还可以将rpc截止日期设置为:

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 )

}

然后在调用任何rpc时使用此函数:

var deadline = getRPCDeadline(1)

grpcClient.hello({message: "abc"},{deadline: deadline}, function(err, response) {
    console.log(err)
    console.log(response)
});

答案 1 :(得分:2)

好吧好像使用下面的代码:

var timeout_in_seconds = 5
var timeout = new Date().setSeconds(new Date().getSeconds() + timeout_in_seconds)

grpcClient.hello({message: "abc"},{deadline: timeout}, function(err, response) {
    console.log(err)
    console.log(response)
});

答案 2 :(得分:0)

https://grpc.io/docs/guides/concepts.html#cancelling-rpcs

截止日期/超时

  

gRPC允许客户指定他们愿意等待的时间   在RPC因错误而终止之前完成的RPC   DEADLINE_EXCEEDED。在服务器端,服务器可以查询是否   特定的RPC已经超时,或者还有多少时间需要完成   RPC。

     

指定截止日期或超时的方式因语言而异   语言 - 例如,并非所有语言都有默认的截止日期,   某些语言API在截止日期(固定点)中起作用   时间),一些语言API在超时方面工作(持续时间)   时间)。

相关问题