forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
92 lines (75 loc) · 1.74 KB
/
registry.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
package ratelimit
import (
"net/http"
"sync"
"time"
"github.com/zalando/skipper/net"
)
const (
DefaultMaxhits = 20
DefaultTimeWindow = 1 * time.Second
DefaultCleanInterval = 60 * time.Second
)
// Registry objects hold the active ratelimiters, ensure synchronized
// access to them, apply default settings and recycle the idle
// ratelimiters.
type Registry struct {
sync.Mutex
defaults Settings
global Settings
//routeSettings map[string]Settings
lookup map[Settings]*Ratelimit
}
// NewRegistry initializes a registry with the provided default settings.
func NewRegistry(settings ...Settings) *Registry {
defaults := Settings{
Type: DisableRatelimit,
MaxHits: DefaultMaxhits,
TimeWindow: DefaultTimeWindow,
CleanInterval: DefaultCleanInterval,
}
r := &Registry{
defaults: defaults,
global: defaults,
lookup: make(map[Settings]*Ratelimit),
}
if len(settings) > 0 {
r.global = settings[0]
}
return r
}
func (r *Registry) get(s Settings) *Ratelimit {
r.Lock()
defer r.Unlock()
rl, ok := r.lookup[s]
if !ok {
rl = newRatelimit(s)
r.lookup[s] = rl
}
return rl
}
// Get returns a Ratelimit instance for provided Settings
func (r *Registry) Get(s Settings) *Ratelimit {
if s.Type == DisableRatelimit || s.Type == NoRatelimit {
return nil
}
return r.get(s)
}
// Check returns Settings used and false in case of request is
// ratelimitted and false otherwise.
func (r *Registry) Check(req *http.Request) (Settings, bool) {
if r == nil {
return Settings{}, true
}
s := r.global
switch s.Type {
case ServiceRatelimit:
return s, r.Get(s).Allow("")
case LocalRatelimit:
ip := net.RemoteHost(req)
if !r.Get(s).Allow(ip.String()) {
return s, false
}
}
return Settings{}, true
}