Use retryPolicy with python GRPC client
Asked Answered
E

1

10

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 ?

Easton answered 6/10, 2020 at 13:53 Comment(0)
A
9

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.

Astrolabe answered 10/12, 2020 at 21:43 Comment(2)
Where in the proto file can I find the <package> and <service>? Don't see these names there.Dowery
From stackoverflow.com/a/75561467, you can use "name": [{}] to match all RPCs.Hosanna

© 2022 - 2024 — McMap. All rights reserved.