在节点js中为grpc函数设置超时 [英] Setting timeouts for grpc functions in node js
本文介绍了在节点js中为grpc函数设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在服务器grpc实现未指定回调函数的情况下尝试为grpc连接创建超时,但是无论选择(new Date().getSeconds()+ 5 )客户端不会终止连接
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
})
推荐答案
您还可以将rpc截止日期设置为:
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 )
}
然后在调用任何rpc时使用此函数:
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)
});
这篇关于在节点js中为grpc函数设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文