|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "time" |
| 6 | + |
| 7 | + "google.golang.org/grpc/codes" |
| 8 | +) |
| 9 | + |
| 10 | +//revive:enable:var-naming |
| 11 | + |
| 12 | +type RetryConfig struct { |
| 13 | + mc map[nameConfig]*methodConfig |
| 14 | +} |
| 15 | + |
| 16 | +type RetryOption func(c *RetryConfig) |
| 17 | + |
| 18 | +type grpcRetryPolicy struct { |
| 19 | + MethodConfig []*methodConfig `json:"methodConfig"` |
| 20 | +} |
| 21 | + |
| 22 | +type methodConfig struct { |
| 23 | + NameConfig []nameConfig `json:"name"` |
| 24 | + RetryPolicy retryPolicy `json:"retryPolicy"` |
| 25 | + RetryThrottling retryThrottling `json:"retryThrottling"` |
| 26 | + WaitForReady bool `json:"waitForReady"` |
| 27 | +} |
| 28 | + |
| 29 | +type nameConfig struct { |
| 30 | + Service string `json:"service,omitempty"` |
| 31 | + Method string `json:"method,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type retryPolicy struct { |
| 35 | + MaxAttempts int `json:"maxAttempts"` |
| 36 | + InitialBackoff Duration `json:"initialBackoff"` |
| 37 | + MaxBackoff Duration `json:"maxBackoff"` |
| 38 | + BackoffMultiplier float64 `json:"backoffMultiplier"` |
| 39 | + RetryableStatusCodes []string `json:"retryableStatusCodes"` |
| 40 | +} |
| 41 | + |
| 42 | +type retryThrottling struct { |
| 43 | + MaxTokens int `json:"maxTokens"` |
| 44 | + TokenRatio float64 `json:"tokenRatio"` |
| 45 | +} |
| 46 | + |
| 47 | +type GRPCKeepAliveConfig struct { |
| 48 | + Time time.Duration `yaml:"time" validate:"required"` |
| 49 | + Timeout time.Duration `yaml:"timeout" validate:"required"` |
| 50 | + PermitWithoutStream bool `yaml:"permit_without_stream"` |
| 51 | +} |
| 52 | + |
| 53 | +func DefaultRetryConfig() *RetryConfig { |
| 54 | + return &RetryConfig{} |
| 55 | +} |
| 56 | + |
| 57 | +func DefaultNameConfig() nameConfig { |
| 58 | + return nameConfig{} |
| 59 | +} |
| 60 | + |
| 61 | +func NewNameConfig(service, method string) nameConfig { |
| 62 | + return nameConfig{ |
| 63 | + Service: service, |
| 64 | + Method: method, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func WithDefaultRetryConfig() RetryOption { |
| 69 | + return func(c *RetryConfig) { |
| 70 | + c = defaultRetryConfig() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func WithRetries(nm nameConfig, n int) RetryOption { |
| 75 | + return func(c *RetryConfig) { |
| 76 | + if c == nil { |
| 77 | + c = defaultRetryConfig() |
| 78 | + } |
| 79 | + |
| 80 | + if _, ok := c.mc[nm]; !ok { |
| 81 | + c.mc[nm] = defaultMethodConfig() |
| 82 | + c.mc[nm].NameConfig = []nameConfig{nm} |
| 83 | + } |
| 84 | + |
| 85 | + c.mc[nm].RetryPolicy.MaxAttempts = n |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func WithRetryableStatusCodes(nm nameConfig, codes ...codes.Code) RetryOption { |
| 90 | + return func(c *RetryConfig) { |
| 91 | + if c == nil { |
| 92 | + c = defaultRetryConfig() |
| 93 | + } |
| 94 | + |
| 95 | + names := make([]string, len(codes)) |
| 96 | + for i, code := range codes { |
| 97 | + names[i] = strings.ToUpper(code.String()) |
| 98 | + } |
| 99 | + |
| 100 | + if _, ok := c.mc[nm]; !ok { |
| 101 | + c.mc[nm] = defaultMethodConfig() |
| 102 | + c.mc[nm].NameConfig = []nameConfig{nm} |
| 103 | + } |
| 104 | + |
| 105 | + c.mc[nm].RetryPolicy.RetryableStatusCodes = names |
| 106 | + } |
| 107 | +} |
0 commit comments