forked from open-feature/go-sdk-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_config.go
More file actions
66 lines (56 loc) · 1.77 KB
/
Copy pathgrpc_config.go
File metadata and controls
66 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package process
import (
"encoding/json"
"fmt"
"google.golang.org/grpc/codes"
"time"
)
const (
// Default timeouts and retry intervals
defaultKeepaliveTime = 30 * time.Second
defaultKeepaliveTimeout = 5 * time.Second
)
type RetryPolicy struct {
MaxAttempts int `json:"MaxAttempts"`
InitialBackoff string `json:"InitialBackoff"`
MaxBackoff string `json:"MaxBackoff"`
BackoffMultiplier float64 `json:"BackoffMultiplier"`
RetryableStatusCodes []string `json:"RetryableStatusCodes"`
}
func (g *Sync) buildRetryPolicy() string {
var policy = map[string]interface{}{
"methodConfig": []map[string]interface{}{
{
"name": []map[string]string{
{"service": "flagd.sync.v1.FlagSyncService"},
},
"retryPolicy": RetryPolicy{
MaxAttempts: 3,
InitialBackoff: "1s",
MaxBackoff: "5s",
BackoffMultiplier: 2.0,
RetryableStatusCodes: []string{"UNKNOWN", "UNAVAILABLE"},
},
},
},
}
retryPolicyBytes, _ := json.Marshal(policy)
retryPolicy := string(retryPolicyBytes)
return retryPolicy
}
// Set of non-retryable gRPC status codes for faster lookup
var nonRetryableCodes map[codes.Code]struct{}
// initNonRetryableStatusCodesSet initializes the set of non-retryable gRPC status codes for quick lookup
func (g *Sync) initNonRetryableStatusCodesSet() {
nonRetryableCodes = make(map[codes.Code]struct{})
for _, codeStr := range g.FatalStatusCodes {
// Wrap the string in quotes to match the expected JSON format
jsonStr := fmt.Sprintf(`"%s"`, codeStr)
var code codes.Code
if err := code.UnmarshalJSON([]byte(jsonStr)); err != nil {
g.Logger.Warn(fmt.Sprintf("unknown status code: %s, error: %v", codeStr, err))
continue
}
nonRetryableCodes[code] = struct{}{}
}
}