-
Notifications
You must be signed in to change notification settings - Fork 342
/
ratelimiterflags.go
104 lines (87 loc) · 2.48 KB
/
ratelimiterflags.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
103
104
package config
import (
"errors"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/ratelimit"
)
const ratelimitsUsage = `set global rate limit settings, e.g. -ratelimits type=client,max-hits=20,time-window=60s
possible ratelimit properties:
type: client/service/clusterClient/clusterService/disabled (defaults to disabled)
max-hits: the number of hits a ratelimiter can get
time-window: the duration of the sliding window for the rate limiter
group: defines the ratelimit group, which can be the same for different routes.
(see also: https://godoc.org/github.com/zalando/skipper/ratelimit)`
const enableRatelimitsUsage = `enable ratelimits`
type ratelimitFlags []ratelimit.Settings
var errInvalidRatelimitConfig = errors.New("invalid ratelimit config (allowed values are: client, service or disabled)")
func (r ratelimitFlags) String() string {
s := make([]string, len(r))
for i, ri := range r {
s[i] = ri.String()
}
return strings.Join(s, "\n")
}
func (r *ratelimitFlags) Set(value string) error {
var s ratelimit.Settings
vs := strings.Split(value, ",")
for _, vi := range vs {
k, v, found := strings.Cut(vi, "=")
if !found {
return errInvalidRatelimitConfig
}
switch k {
case "type":
switch v {
case "local":
log.Warning("LocalRatelimit is deprecated, please use ClientRatelimit instead")
fallthrough
case "client":
s.Type = ratelimit.ClientRatelimit
case "service":
s.Type = ratelimit.ServiceRatelimit
case "clusterClient":
s.Type = ratelimit.ClusterClientRatelimit
case "clusterService":
s.Type = ratelimit.ClusterServiceRatelimit
case "disabled":
s.Type = ratelimit.DisableRatelimit
default:
return errInvalidRatelimitConfig
}
case "max-hits":
i, err := strconv.Atoi(v)
if err != nil {
return err
}
s.MaxHits = i
case "time-window":
d, err := time.ParseDuration(v)
if err != nil {
return err
}
s.TimeWindow = d
s.CleanInterval = d * 10
case "group":
s.Group = v
default:
return errInvalidRatelimitConfig
}
}
if s.Type == ratelimit.NoRatelimit {
s.Type = ratelimit.DisableRatelimit
}
*r = append(*r, s)
return nil
}
func (r *ratelimitFlags) UnmarshalYAML(unmarshal func(interface{}) error) error {
var rateLimitSettings ratelimit.Settings
if err := unmarshal(&rateLimitSettings); err != nil {
return err
}
rateLimitSettings.CleanInterval = rateLimitSettings.TimeWindow * 10
*r = append(*r, rateLimitSettings)
return nil
}