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
18go run server/main.go
19```
20
21Then run the client.  Note that when running the client, `GRPC_GO_RETRY=on` must be set in
22your environment:
23
24```bash
25GRPC_GO_RETRY=on go run client/main.go
26```
27
28## Usage
29
30### Define your retry policy
31
32Retry is enabled via the service config, which can be provided by the name resolver or
33a DialOption (described below).  In the below config, we set retry policy for the
34"grpc.example.echo.Echo" method.
35
36MaxAttempts: how many times to attempt the RPC before failing.
37InitialBackoff, MaxBackoff, BackoffMultiplier: configures delay between attempts.
38RetryableStatusCodes: Retry only when receiving these status codes.
39
40```go
41        var retryPolicy = `{
42            "methodConfig": [{
43                // config per method or all methods under service
44                "name": [{"service": "grpc.examples.echo.Echo"}],
45                "waitForReady": true,
46
47                "retryPolicy": {
48                    "MaxAttempts": 4,
49                    "InitialBackoff": ".01s",
50                    "MaxBackoff": ".01s",
51                    "BackoffMultiplier": 1.0,
52                    // this value is grpc code
53                    "RetryableStatusCodes": [ "UNAVAILABLE" ]
54                }
55            }]
56        }`
57```
58
59### Providing the retry policy as a DialOption
60
61To use the above service config, pass it with `grpc.WithDefaultServiceConfig` to
62`grpc.Dial`.
63
64```go
65conn, err := grpc.Dial(ctx,grpc.WithInsecure(), grpc.WithDefaultServiceConfig(retryPolicy))
66```
67