-
Notifications
You must be signed in to change notification settings - Fork 52
/
limiter.go
69 lines (61 loc) · 1.39 KB
/
limiter.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
package limiter
import (
"net/http"
"sort"
"time"
"github.com/sohaha/zlsgo/znet"
)
// Rule user access control strategy
type Rule struct {
rules []*singleRule
}
// New New limiter
func New(allowed uint64, overflow ...func(c *znet.Context)) znet.HandlerFunc {
r := NewRule()
f := func(c *znet.Context) {
c.String(http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests))
}
if len(overflow) > 0 {
f = overflow[0]
}
r.AddRule(time.Second, int(allowed))
return func(c *znet.Context) {
if !r.AllowVisitByIP(c.GetClientIP()) {
f(c)
return
}
c.Next()
}
}
// NewRule Custom limiter rule
func NewRule() *Rule {
return &Rule{}
}
// AddRule increase user access control strategy
// If less than 1s, please use golang.org/x/time/rate
func (r *Rule) AddRule(exp time.Duration, allowed int, estimated ...int) {
r.rules = append(r.rules, newRule(exp, allowed, estimated...))
sort.Slice(r.rules, func(i int, j int) bool {
return r.rules[i].defaultExpiration < r.rules[j].defaultExpiration
})
}
// AllowVisit Is access allowed
func (r *Rule) AllowVisit(key interface{}) bool {
if len(r.rules) == 0 {
return true
}
for i := range r.rules {
if !r.rules[i].allowVisit(key) {
return false
}
}
return true
}
// AllowVisitByIP AllowVisit IP
func (r *Rule) AllowVisitByIP(ip string) bool {
i, _ := znet.IPToLong(ip)
if i == 0 {
return false
}
return r.AllowVisit(i)
}