Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use retryPolicy with python GRPC client

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 ?

like image 242
cp2587 Avatar asked Oct 06 '20 13:10

cp2587


People also ask

How do I work with gRPC in Python?

This tutorial provides a basic Python programmer’s introduction to working with gRPC. By walking through this example you’ll learn how to: Define a service in a .proto file. Generate server and client code using the protocol buffer compiler. Use the Python gRPC API to write a simple client and server for your service.

How do I configure a retry policy in gRPC?

A retry policy is configured once when a gRPC channel is created: Creates a MethodConfig. Retry policies can be configured per-method and methods are matched using the Names property. This method is configured with MethodName.Default, so it's applied to all gRPC methods called by this channel.

What can you do with gRPC?

With gRPC we can define our service once in a. proto file and generate clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside a large data center to your own tablet-all the complexity of communication between different languages and environments is handled for you by gRPC.

What is gRPC retries in NET Core?

ASP. NET Core gRPC retries is a feature that allows gRPC clients to automatically retry failed calls. This article discusses how to configure a retry policy to make resilient, fault tolerant gRPC apps in .NET. gRPC retries requires Grpc.Net.Client version 2.36.0 or later. gRPC calls can be interrupted by transient faults.


1 Answers

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.

like image 167
Emil Hansen Avatar answered Oct 17 '22 11:10

Emil Hansen