-
Notifications
You must be signed in to change notification settings - Fork 1
/
limits.go
78 lines (64 loc) · 1.74 KB
/
limits.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
package genratelimit
import (
"strings"
)
// Limit is the limit applied to specific descriptors
type Limit struct {
Key string
Value *YamlRateLimit
}
type limits []*Limit
// Len returns the length of the limits
func (s limits) Len() int { return len(s) }
// Swap swaps the two limits
func (s limits) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Less returns true if the limit at index i is less than the limit at index j
func (s limits) Less(i, j int) bool {
iKeys := strings.Split(s[i].Key, delimiter)
jKeys := strings.Split(s[j].Key, delimiter)
for idx := range iKeys {
if iKeys[idx] == jKeys[idx] {
continue
}
// "" should be last, this ensure that the most specific tuple set is used
if iKeys[idx] == "" {
return false
}
if jKeys[idx] == "" {
return true
}
return iKeys[idx] < jKeys[idx]
}
return false
}
// Descriptors returns envoyproxy/ratelimit formatted descriptors for the supplied limits
func (s limits) Descriptors(names []string) []*YamlDescriptor {
descriptors := make([]*YamlDescriptor, 0, len(s))
descriptorsMap := make(map[string]*YamlDescriptor)
for _, l := range s {
keys := strings.Split((l.Key), delimiter)
aggregateKey := ""
for i, key := range keys {
prevKey := aggregateKey
aggregateKey = aggregateKey + delimiter + key
var desc *YamlDescriptor
var ok bool
if desc, ok = descriptorsMap[aggregateKey]; !ok {
desc = &YamlDescriptor{
Key: names[i],
Value: key,
}
descriptorsMap[aggregateKey] = desc
if i == 0 {
descriptors = append(descriptors, desc)
} else {
descriptorsMap[prevKey].Descriptors = append(descriptorsMap[prevKey].Descriptors, desc)
}
}
if i == len(keys)-1 {
desc.RateLimit = l.Value
}
}
}
return descriptors
}