README.md
1# Retry
2
3This example shows how to enable and configure retry on gRPC clients.
4
5## Documentation
6
7[gRFC for client-side retry support](https://github.com/grpc/proposal/blob/master/A6-client-retries.md)
8
9## Try it
10
11This example includes a service implementation that fails requests three times with status
12code `Unavailable`, then passes the fourth. The client is configured to make four retry attempts
13when receiving an `Unavailable` status code.
14
15First start the server:
16
17```bash
18$ ./server
19```
20
21Then run the client:
22
23```bash
24$ ./client
25```
26
27Expected server output:
28
29```
30Server listening on 0.0.0.0:50052
31return UNAVAILABLE
32return UNAVAILABLE
33return UNAVAILABLE
34return OK
35```
36
37Expected client output:
38
39```
40Greeter received: Hello world
41```
42
43## Usage
44
45### Define your retry policy
46
47Retry is enabled via the service config, which can be provided by the name resolver or
48a [GRPC_ARG_SERVICE_CONFIG](https://github.com/grpc/grpc/blob/master/include/grpc/impl/channel_arg_names.h#L207-L209) channel argument. In the below config, we set retry policy for the "helloworld.Greeter" service.
49
50`maxAttempts`: how many times to attempt the RPC before failing.
51
52`initialBackoff`, `maxBackoff`, `backoffMultiplier`: configures delay between attempts.
53
54`retryableStatusCodes`: Retry only when receiving these status codes.
55
56```c++
57constexpr absl::string_view kRetryPolicy =
58 "{\"methodConfig\" : [{"
59 " \"name\" : [{\"service\": \"helloworld.Greeter\"}],"
60 " \"waitForReady\": true,"
61 " \"retryPolicy\": {"
62 " \"maxAttempts\": 4,"
63 " \"initialBackoff\": \"1s\","
64 " \"maxBackoff\": \"120s\","
65 " \"backoffMultiplier\": 1.0,"
66 " \"retryableStatusCodes\": [\"UNAVAILABLE\"]"
67 " }"
68 "}]}";
69```
70