-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathql.go
121 lines (102 loc) · 2.94 KB
/
ql.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
package quantitylimiter
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
"net/http"
"strconv"
)
func init() {
caddy.RegisterModule(QuantityLimiter{})
httpcaddyfile.RegisterHandlerDirective("quantity_limiter", parseCaddyfile)
}
// QuantityLimiter limits the number of successful requests for a token and allows the counter to be reset.
type QuantityLimiter struct {
logger *zap.Logger
// Contains tokens and their number of allowed requests left.
counter map[string]uint64
// Parameter used to set a token.
paramSet string
// Parameter used to get a limited resource.
paramGet string
// Prefix to be used for GET parameters for set and get tokens.
ParameterNamePrefix string `json:"parameterNamePrefix,omitempty"`
// Number of successful requests that can be made using a token.
Quantity uint64 `json:"quantity,omitempty"`
}
func (QuantityLimiter) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.quantity_limiter",
New: func() caddy.Module {
return QuantityLimiter{}
},
}
}
func (ql QuantityLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if r.URL.Query().Has(ql.paramSet) {
ql.counter[r.URL.Query().Get(ql.paramSet)] = ql.Quantity
w.WriteHeader(http.StatusAccepted)
return nil
}
if r.URL.Query().Has(ql.paramGet) {
if ql.counter[r.URL.Query().Get(ql.paramGet)] == 0 {
delete(ql.counter, ql.paramGet)
w.WriteHeader(http.StatusNotFound)
return nil
}
ql.counter[r.URL.Query().Get(ql.paramGet)] -= 1
r.Header.Del(ql.paramGet)
}
return next.ServeHTTP(w, r)
}
func (ql *QuantityLimiter) Provision(ctx caddy.Context) error {
ql.logger = ctx.Logger(ql)
ql.counter = make(map[string]uint64)
if ql.ParameterNamePrefix == "" {
ql.ParameterNamePrefix = "ql_"
}
ql.paramSet = ql.ParameterNamePrefix + "set"
ql.paramGet = ql.ParameterNamePrefix + "get"
if ql.Quantity == 0 {
ql.Quantity = 1
}
return nil
}
func (ql *QuantityLimiter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "parameterNamePrefix":
if !d.Args(&ql.ParameterNamePrefix) {
return d.ArgErr()
}
case "quantity":
var quantity string
if !d.Args(&quantity) {
return d.ArgErr()
}
var err error
ql.Quantity, err = strconv.ParseUint(quantity, 10, 32)
if err != nil {
return d.Err(err.Error())
}
default:
return fmt.Errorf("unknown subdirective: %q", d.Val())
}
}
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var ql QuantityLimiter
err := ql.UnmarshalCaddyfile(h.Dispenser)
return ql, err
}
// Interface guards
var (
_ caddy.Provisioner = (*QuantityLimiter)(nil)
_ caddyfile.Unmarshaler = (*QuantityLimiter)(nil)
)