问题描述
我已经非常努力地使用GRPC文档的embed retryPolicy( https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy ),但是我不明白我应该在哪里设置配置我的代码.
理想情况下,我希望python客户端指定其重试策略,但我也想了解如何从服务器端进行管理.
I have tried really hard to use the embed retryPolicy of GRPC documentation (https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy) but i fail to understand where i should setup the config in my code.
Ideally i would like the python client to specify its retry policy but i am also interested to understand how to manage it from the server side.
经过一番挖掘,我想出了这个功能,但它不起作用.
After some digging, i came up with this snipped but it does not work.
import json
from grpc import insecure_channel
service_default_config = {
# see https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy-capabilities
"retryPolicy": {
"maxAttempts": 5,
"initialBackoff": "1s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": [
"RESOURCE_EXHAUSTED",
"UNAVAILABLE"
]
}
}
service_default_config = json.dumps(service_default_config)
options = [
('grpc.service_config', service_default_config)
]
insecure_channel(hostname, options=options)
任何人都可以为我指出相关文档,以帮助我理解其工作原理或向我解释我的误会吗?
Can anyone point me out the relevant documentation for me to understand how this works or explain to me what i misunderstand ?
推荐答案
我在配置语法上也遇到了同样的问题.
I had the same problem with the syntax of the config.
我发现了此.
简而言之,必须将retryPolicy指定为methodConfig的一部分,该方法描述了如何处理对特定服务的调用:
In short, the retryPolicy has to be specified as part of a methodConfig which describes how to handle calls to a specific service:
import json
import grpc
json_config = json.dumps(
{
"methodConfig": [
{
"name": [{"service": "<package>.<service>"}],
"retryPolicy": {
"maxAttempts": 5,
"initialBackoff": "0.1s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"],
},
}
]
}
)
address = 'localhost:50051'
channel = grpc.insecure_channel(address, options=[("grpc.service_config", json_config)])
其中在原始文件中定义了< package>
和< service>
.
where <package>
and <service>
are defined in your proto file.
这篇关于将retryPolicy与python GRPC客户端一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!