<!-- If you know who should review your pull request, please assign it to that person, otherwise the pull request would get assigned randomly. If your pull request is for a specific language, please add the appropriate lang label. --> Closes #37034 PiperOrigin-RevId: 646276954
1.7 KiB
Retry
This example shows how to enable and configure retry on gRPC clients.
Documentation
gRFC for client-side retry support
Try it
This example includes a service implementation that fails requests three times with status
code Unavailable, then passes the fourth. The client is configured to make four retry attempts
when receiving an Unavailable status code.
First start the server:
$ ./server
Then run the client:
$ ./client
Expected server output:
Server listening on 0.0.0.0:50052
return UNAVAILABLE
return UNAVAILABLE
return UNAVAILABLE
return OK
Expected client output:
Greeter received: Hello world
Usage
Define your retry policy
Retry is enabled via the service config, which can be provided by the name resolver or a GRPC_ARG_SERVICE_CONFIG channel argument. In the below config, we set retry policy for the "helloworld.Greeter" service.
maxAttempts: how many times to attempt the RPC before failing.
initialBackoff, maxBackoff, backoffMultiplier: configures delay between attempts.
retryableStatusCodes: Retry only when receiving these status codes.
constexpr absl::string_view kRetryPolicy =
"{\"methodConfig\" : [{"
" \"name\" : [{\"service\": \"helloworld.Greeter\"}],"
" \"waitForReady\": true,"
" \"retryPolicy\": {"
" \"maxAttempts\": 4,"
" \"initialBackoff\": \"1s\","
" \"maxBackoff\": \"120s\","
" \"backoffMultiplier\": 1.0,"
" \"retryableStatusCodes\": [\"UNAVAILABLE\"]"
" }"
"}]}";