Use retryPolicy with python GRPC client

ぐ巨炮叔叔 提交于 2021-01-27 12:42:59

问题


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 ?


回答1:


I had the same problem with the syntax of the config.

I found this.

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

where <package> and <service> are defined in your proto file.



来源:https://stackoverflow.com/questions/64227270/use-retrypolicy-with-python-grpc-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!