-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
180 lines (149 loc) · 4.45 KB
/
plugin.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
171
172
173
174
175
176
177
178
179
180
package gatewayflowcontroller
import (
"context"
"fmt"
"net/http"
"net/rpc"
"os"
"time"
"github.com/RSS3-Network/gatewayflowcontroller/types"
)
// Config the plugin configuration.
type Config struct {
// Request related
KeyHeader string `json:"key_header,omitempty"`
// Connector
ConnectorRPC string `json:"connector_rpc,omitempty"`
// Rate Limit
MaxSources int `json:"max_sources,omitempty"`
Average int64 `json:"average,omitempty"`
Period int64 `json:"period,omitempty"`
Burst int64 `json:"burst,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
KeyHeader: "X-API-Key", // Default header
ConnectorRPC: "", // Disable connector
MaxSources: 65535,
Average: 20,
Period: 60,
Burst: 60,
}
}
type Core struct {
next http.Handler
name string
rateLimiter *RateLimiter
}
// FlowController : a FlowController plugin.
type FlowController struct {
Core
keyHeader string
connector *rpc.Client
}
// New created a new plugin.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
var (
connector *rpc.Client
err error
)
// Whether to use full mode
fullMode := true
if config.ConnectorRPC != "" {
connector, err = rpc.Dial("tcp", config.ConnectorRPC)
if err != nil {
return nil, fmt.Errorf("init connector: %w", err)
}
} else {
_, _ = os.Stderr.WriteString("no connector rpc found, skip connector init\n")
fullMode = false
}
// Initialize rate limiter
rateLimiter, err := NewRateLimiter(config.MaxSources, config.Average, config.Period, config.Burst)
if err != nil {
return nil, fmt.Errorf("init rate limiter: %w", err)
}
core := Core{
next: next,
name: name,
rateLimiter: rateLimiter,
}
_, _ = os.Stdout.WriteString("gateway flow-controller start\n")
if !fullMode {
// Only enable core mode
_, _ = os.Stderr.WriteString("fallback to core mode\n")
return &core, nil
}
// Enable full mode
return &FlowController{
Core: core,
keyHeader: config.KeyHeader,
connector: connector,
}, nil
}
// The Core processor
func (fc *Core) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if fc.rateLimiter.RateLimit(rw, req, nil) {
fc.next.ServeHTTP(rw, req)
}
}
// The Full processor
func (fc *FlowController) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Prepare the access log
l := types.AccesslogProduceLogArgs{
Path: req.URL.Path,
Timestamp: time.Now(),
}
// To get response code, we have to proxy the response writer.
rwp := NewResponseWriterProxy(rw)
// Whether proceed to next middleware
proceed := true
if key := req.Header.Get(fc.keyHeader); key == "" {
// No key provided, use common rate limit
proceed = fc.rateLimiter.RateLimit(rwp, req, nil)
} else {
// Query key
var checkKeyReply types.ControlCheckKeyReply
if err := fc.connector.Call("Connector.ControlCheckKey", &types.ControlCheckKeyArgs{
Key: key,
}, &checkKeyReply); err != nil {
// Key provided, but failed to check, apply rate limit
_, _ = os.Stderr.WriteString(fmt.Sprintf("check key %s: %v\n", key, err))
proceed = fc.rateLimiter.RateLimit(rwp, req, nil)
} else if checkKeyReply.Account == nil {
// Key check succeeded, but no such key
proceed = fc.rateLimiter.RateLimit(rwp, req, nil)
} else {
// Has valid key, record it
l.KeyID = checkKeyReply.KeyID
var checkAccountPausedReply types.CheckAccountPausedReply
// Check whether the account of this key has been paused
if err := fc.connector.Call("Connector.ControlCheckAccountPaused", &types.CheckAccountPausedArgs{
Account: *checkKeyReply.Account,
}, &checkAccountPausedReply); err != nil {
// Failed to check account, skip rate limit
_, _ = os.Stderr.WriteString(fmt.Sprintf("check account %s: %v\n", *checkKeyReply.Account, err))
} else if checkAccountPausedReply.IsPaused {
// Account paused, apply rate limit by account
proceed = fc.rateLimiter.RateLimit(rwp, req, checkKeyReply.Account)
}
}
}
// Should we proceed or abort ?
if proceed {
fc.next.ServeHTTP(rwp, req)
}
// Produce log
l.Status = rwp.StatusCode()
go func() {
l := l
if err := fc.connector.Call("Connector.AccesslogProduceLog", &l, nil); err != nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("produce log %v: %v\n", l, err))
}
}()
}
// Stop will not be called, it's just a resource release reminder
func (fc *FlowController) Stop() {
_ = fc.connector.Close()
}