1 package retry
2 3 import (
4 "encoding/json"
5 "fmt"
6 7 "google.golang.org/grpc"
8 )
9 10 // RetryDialOption returns DialOption that configures the default
11 // service config, which will be used in cases where:
12 //
13 // 1. User provide service config in SDK build after RetryDialOption function.
14 // In this case retries configuration override user's service config.
15 //
16 // 2. User don't get service config through xDS. In that case service config
17 // from xDS override retries configuration.
18 //
19 // It's recommended to use default configuration (DefaultRetryDialOption)
20 // for retries in SDK to avoid retry amplification.
21 func RetryDialOption(opts ...RetryOption) (grpc.DialOption, error) {
22 config := defaultRetryConfig()
23 for _, opt := range opts {
24 opt(config)
25 }
26 27 if config.mc == nil {
28 return nil, fmt.Errorf("can't provide retry config with this options")
29 }
30 31 mc := []*methodConfig{}
32 for _, v := range config.mc {
33 mc = append(mc, v)
34 }
35 36 c, err := json.Marshal(&grpcRetryPolicy{MethodConfig: mc, RetryThrottling: *config.retryThrottling})
37 if err != nil {
38 return nil, err
39 }
40 41 return grpc.WithDefaultServiceConfig(string(c)), nil
42 }
43 44 // DefaultRetryDialOption returns DialOption that configures the default
45 // service config, which will be used in cases where:
46 //
47 // 1. User provide service config in SDK build after DefaultRetryDialOption function.
48 // In this case retries configuration override user's service config.
49 //
50 // 2. User don't get service config through xDS. In that case service config
51 // from xDS override retries configuration.
52 func DefaultRetryDialOption() (grpc.DialOption, error) {
53 return RetryDialOption(WithDefaultRetryConfig())
54 }
55