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\"]"
" }"
"}]}";