forked from kubernetes-sigs/aws-load-balancer-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
102 lines (93 loc) · 3 KB
/
config.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package throttle
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"golang.org/x/time/rate"
"regexp"
"sort"
"strconv"
"strings"
)
type throttleConfig struct {
operationPtn *regexp.Regexp
r rate.Limit
burst int
}
var _ pflag.Value = &ServiceOperationsThrottleConfig{}
// serviceOperationsThrottleConfig is throttleConfig for each service's operations.
// It supports to be configured using flags with format like "${serviceID}:${operationRegex}={rate}:{burst}"
// e.g. "appmesh:DescribeMesh=1.3:5,appmesh:Create.*=1.7:3"
// Note: default throttle for each service will be cleared if any override is set for that service.
type ServiceOperationsThrottleConfig struct {
// service:operationRegex:config
value map[string][]throttleConfig
}
func (c *ServiceOperationsThrottleConfig) String() string {
if c == nil {
return ""
}
var configs []string
var serviceIDs []string
for serviceID := range c.value {
serviceIDs = append(serviceIDs, serviceID)
}
sort.Strings(serviceIDs)
for _, serviceID := range serviceIDs {
for _, operationsThrottleConfig := range c.value[serviceID] {
configs = append(configs, fmt.Sprintf("%s:%s=%v:%d",
serviceID,
operationsThrottleConfig.operationPtn.String(),
operationsThrottleConfig.r,
operationsThrottleConfig.burst,
))
}
}
return strings.Join(configs, ",")
}
func (c *ServiceOperationsThrottleConfig) Set(val string) error {
valueOverride := make(map[string][]throttleConfig)
configPairs := strings.Split(val, ",")
for _, pair := range configPairs {
kv := strings.Split(pair, "=")
if len(kv) != 2 {
return errors.Errorf("%s must be formatted as serviceID:operationRegex=rate:burst", pair)
}
serviceIDOperationRegexPair := strings.Split(kv[0], ":")
if len(serviceIDOperationRegexPair) != 2 {
return errors.Errorf("%s must be formatted as serviceID:operationRegex", kv[0])
}
rateBurstPair := strings.Split(kv[1], ":")
if len(rateBurstPair) != 2 {
return errors.Errorf("%s must be formatted as rate:burst", kv[1])
}
serviceID := serviceIDOperationRegexPair[0]
operationPtn, err := regexp.Compile(serviceIDOperationRegexPair[1])
if err != nil {
return errors.Errorf("%s must be valid regex expression for operation", serviceIDOperationRegexPair[1])
}
r, err := strconv.ParseFloat(rateBurstPair[0], 64)
if err != nil {
return errors.Errorf("%s must be valid float number as rate for operations per second", rateBurstPair[0])
}
burst, err := strconv.Atoi(rateBurstPair[1])
if err != nil {
return errors.Errorf("%s must be valid integer as burst for operations", rateBurstPair[1])
}
valueOverride[serviceID] = append(valueOverride[serviceID], throttleConfig{
operationPtn: operationPtn,
r: rate.Limit(r),
burst: burst,
})
}
if c.value == nil {
c.value = make(map[string][]throttleConfig)
}
for k, v := range valueOverride {
c.value[k] = v
}
return nil
}
func (c *ServiceOperationsThrottleConfig) Type() string {
return "serviceOperationsThrottleConfig"
}