-
Notifications
You must be signed in to change notification settings - Fork 3
/
limit.go
170 lines (137 loc) · 3.53 KB
/
limit.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package throttleplugin
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/logp"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type RemoteConfig struct {
Key string `yaml:"key"`
DefaultLimit int64 `yaml:"default_limit"`
Rules []RuleConfig `yaml:"rules"`
}
type RuleConfig struct {
Limit int64 `yaml:"limit"`
Selectors map[string]string `yaml:"selectors"`
}
type RemoteLimiter struct {
url string
client *http.Client
bucketInterval int64
buckets int64
mu sync.RWMutex
key string
rules []Rule
limiters map[string]*BucketLimiter
}
// NewRemoteLimiter creates new remote limiter instance.
func NewRemoteLimiter(url string, bucketInterval, buckets int64) (*RemoteLimiter, error) {
rl := &RemoteLimiter{
url: url,
client: http.DefaultClient,
bucketInterval: bucketInterval,
buckets: buckets,
limiters: make(map[string]*BucketLimiter),
}
return rl, nil
}
// Allow returns TRUE if event is allowed to be processed.
func (rl *RemoteLimiter) Allow(e *beat.Event) bool {
var ts time.Time
if tsString, err := e.GetValue("ts"); err == nil {
ts, _ = time.Parse(time.RFC3339, tsString.(string))
}
if ts.IsZero() {
ts = time.Now()
}
keyValue, _ := e.GetValue(rl.key)
kv := fmt.Sprintf("%v", keyValue)
rl.mu.Lock()
defer rl.mu.Unlock()
for _, r := range rl.rules {
if matched, key := r.Match(e); matched {
key = kv + key
// check if we already have limiter
limiter, ok := rl.limiters[key]
if !ok {
limiter = NewBucketLimiter(rl.bucketInterval, r.Limit(), rl.buckets, ts)
rl.limiters[key] = limiter
}
return limiter.Allow(ts)
}
}
return true
}
// Update retrieves policies from Policy Manager.
func (rl *RemoteLimiter) Update(ctx context.Context) error {
r, err := http.NewRequest("GET", rl.url, nil)
if err != nil {
return errors.Wrap(err, "failed to create request")
}
res, err := rl.client.Do(r)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
var c RemoteConfig
if err := yaml.Unmarshal(body, &c); err != nil {
return errors.Wrap(err, "failed to unpack config")
}
rules := make([]Rule, 0, len(c.Rules)+1)
for _, l := range c.Rules {
rules = append(rules, NewRule(l.Selectors, l.Limit))
}
defaultRule := NewRule(map[string]string{}, c.DefaultLimit)
rules = append(rules, defaultRule)
limiterTTL := time.Duration(rl.bucketInterval*rl.buckets) * time.Second
limiterThreshold := time.Now().Add(-limiterTTL)
rl.mu.Lock()
defer rl.mu.Unlock()
rl.key = c.Key
rl.rules = rules
for id, l := range rl.limiters {
if l.LastUpdate().Before(limiterThreshold) {
delete(rl.limiters, id)
}
}
return nil
}
// UpdateWithInterval runs update with some interval.
func (rl *RemoteLimiter) UpdateWithInterval(ctx context.Context, interval time.Duration) error {
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
if err := rl.Update(ctx); err != nil {
logp.Err("failed to update limit policies: %v", err)
}
}
}
}
func (rl *RemoteLimiter) WriteStatus(w io.Writer) error {
rl.mu.Lock()
defer rl.mu.Unlock()
for key, cl := range rl.limiters {
fmt.Fprintf(w, "#%v\n\n", key)
if err := cl.WriteStatus(w); err != nil {
return err
}
fmt.Fprintln(w, "---------")
}
fmt.Fprintf(w, "rules: \n\n%#v", rl.rules)
return nil
}