Use retryPolicy with python GRPC client

5.8k views Asked by At

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

There are 1 answers

1
Emil Hansen On

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.