【问题标题】:Is python grpc client support retry?python grpc客户端支持重试吗?
【发布时间】:2021-01-18 09:01:02
【问题描述】:

我在Python中使用grpc,发现两个节点之间的通信不小心遇到了StatusCode.UNAVAILABLE。

我找到了一个解决方案,它说 UNAVAILABLE 是一个可重试的错误,我们应该重试:https://github.com/grpc/grpc/issues/16515

所以我查阅了文档,发现了这个:https://github.com/grpc/proposal/blob/master/A6-client-retries.md。本文档显示了一个配置演示,如下所示。

"retryPolicy": {
  "maxAttempts": 4,
  "initialBackoff": "0.1s",
  "maxBackoff": "1s",
  "backoffMultiplier": 2,
  "retryableStatusCodes": [
    "UNAVAILABLE"
  ]
}

我尝试按照此问题中的两个示例进行操作,但仍然无法正常工作:Use retryPolicy with python GRPC client

这是我的代码,这里还有另一个问题。我不太明白“。”的意思。 :

json_config = json.dumps(
                {
                    "methodConfig": [
                        {
                            # "name": [{"service": "<package>.<service>"}],
                            "retryPolicy": {
                                "maxAttempts": 5,
                                "initialBackoff": "0.1s",
                                "maxBackoff": "10s",
                                "backoffMultiplier": 2,
                                "retryableStatusCodes": ["UNAVAILABLE"],
                            },
                        }
                    ]
                }
            )

            options = [
                ('grpc.service_config', json_config)
            ]
            taf_grpc_client = GrpcRpcClient(RpcConfig(taf_server_host, self._taf_server_port, options=options),
                                            taf_server_proto_pb2_grpc.TafServerStub)
            self._taf_grpc_client_dict[taf_server_host] = taf_grpc_client

我想知道的是Python GRPC是否支持“重试”,它的正确用法是什么。

【问题讨论】:

    标签: grpc-python retrypolicy


    【解决方案1】:

    您指定的服务配置是正确的。由于没有复制案例,我将您的代码应用于我们的 HelloWorld 示例:

    async def run() -> None:
        json_config = json.dumps({
            "methodConfig": [{
                "name": [{
                    "service": "helloworld.Greeter"
                }],
                "retryPolicy": {
                    "maxAttempts": 5,
                    "initialBackoff": "0.1s",
                    "maxBackoff": "10s",
                    "backoffMultiplier": 2,
                    "retryableStatusCodes": ["UNAVAILABLE"],
                },
            }]
        })
        async with grpc.aio.insecure_channel('localhost:50051',
                                             options=(('grpc.service_config',
                                                       json_config),)) as channel:
            stub = helloworld_pb2_grpc.GreeterStub(channel)
            response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
        print("Greeter client received: " + response.message)
    

    如果您使用 env GRPC_VERBOSITY=debug 运行它,您应该观察到多次尝试重试连接。如果还有其他问题,请向https://github.com/grpc/grpc/issues提出问题。

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2022-11-30
      • 1970-01-01
      • 2021-09-27
      • 2020-09-09
      • 2019-03-28
      相关资源
      最近更新 更多