-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathbackendratelimit_test.go
227 lines (197 loc) · 6 KB
/
backendratelimit_test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package proxy_test
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters/builtin"
ratelimitfilters "github.com/zalando/skipper/filters/ratelimit"
snet "github.com/zalando/skipper/net"
"github.com/zalando/skipper/net/redistest"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/proxy/proxytest"
"github.com/zalando/skipper/ratelimit"
)
type (
countingBackend struct {
server *httptest.Server
requests int
}
countingBackends []*countingBackend
)
func (b *countingBackend) ServeHTTP(http.ResponseWriter, *http.Request) {
b.requests++
}
func (b *countingBackend) String() string {
return b.server.URL
}
func newCountingBackend() *countingBackend {
b := &countingBackend{}
b.server = httptest.NewServer(b)
return b
}
func newCountingBackends(n int) (result countingBackends) {
for i := 0; i < n; i++ {
result = append(result, newCountingBackend())
}
return
}
func (backends countingBackends) close() {
for _, b := range backends {
b.server.Close()
}
}
func (backends countingBackends) endpoints() (result []string) {
for _, b := range backends {
result = append(result, b.server.URL)
}
return
}
func (backends countingBackends) String() string {
var urls []string
for _, e := range backends.endpoints() {
urls = append(urls, `"`+e+`"`)
}
return strings.Join(urls, ",")
}
// Round robin distributes requests evenly between backends,
// test each backend gets exactly max hits before limit kicks-in
func TestBackendRatelimitRoundRobin(t *testing.T) {
const (
nBackends = 3
maxHits = 7
timeWindow = 10 * time.Second
)
filterRegistry := builtin.MakeRegistry()
filterRegistry.Register(ratelimitfilters.NewBackendRatelimit())
redisAddr, done := redistest.NewTestRedis(t)
defer done()
ratelimitRegistry := ratelimit.NewSwarmRegistry(nil, &snet.RedisOptions{Addrs: []string{redisAddr}})
defer ratelimitRegistry.Close()
backends := newCountingBackends(nBackends)
defer backends.close()
doc := fmt.Sprintf(`* -> backendRatelimit("testapi", %d, "%s") -> <roundRobin, %v>`, maxHits, timeWindow.String(), backends)
r := eskip.MustParse(doc)
p := proxytest.WithParams(filterRegistry, proxy.Params{RateLimiters: ratelimitRegistry}, r...)
defer p.Close()
const totalMaxHits = nBackends * maxHits
requestAndExpect(t, p.URL, totalMaxHits, http.StatusOK, nil)
requestAndExpect(t, p.URL, 1, http.StatusServiceUnavailable, http.Header{"Content-Length": []string{"0"}})
for _, b := range backends {
if b.requests != maxHits {
t.Errorf("Expected %d hits for backend %s, got: %d", maxHits, b, b.requests)
}
}
}
func TestBackendRatelimitScenarios(t *testing.T) {
// Use shared instance for all tests.
// If this test flakes because of backend IP reuse between test cases,
// implement database cleanup, see https://redis.io/commands/flushdb/.
redisAddr, done := redistest.NewTestRedis(t)
defer done()
for _, ti := range []struct {
name string
routes string
backends int
requests map[string]int
maxHits int
rejectStatusCode int
}{
{
"single route with one backend",
`* -> backendRatelimit("testapi", 7, "10s") -> $backends`,
1,
map[string]int{"/": 10},
7,
http.StatusServiceUnavailable,
},
{
"single route with one backend and status override",
`* -> backendRatelimit("testapi", 7, "10s", 429) -> $backends`,
1,
map[string]int{"/": 10},
7,
http.StatusTooManyRequests,
},
{
"single route with three backends, random",
`* -> backendRatelimit("testapi", 7, "10s") -> <random, $backends>`,
3,
map[string]int{"/": 30},
7,
http.StatusServiceUnavailable,
},
{
"single route with three backends, roundRobin",
`* -> backendRatelimit("testapi", 7, "10s") -> <roundRobin, $backends>`,
3,
map[string]int{"/": 30},
7,
http.StatusServiceUnavailable,
},
{
"single route with three backends, consistentHash",
`* -> backendRatelimit("testapi", 7, "10s") -> <consistentHash, $backends>`,
3,
map[string]int{"/": 30},
7,
http.StatusServiceUnavailable,
},
{
"two routes with three backends and common limit",
`api1: Path("/api1") -> backendRatelimit("api", 7, "10s") -> <random, $backends>;
api2: Path("/api2") -> backendRatelimit("api", 7, "10s") -> <random, $backends>`,
3,
map[string]int{"/api1": 15, "/api2": 15},
7,
http.StatusServiceUnavailable,
},
{
"two routes with three backends and separate limits",
`api1: Path("/api1") -> backendRatelimit("api1", 4, "10s") -> <random, $backends>;
api2: Path("/api2") -> backendRatelimit("api2", 8, "10s") -> <random, $backends>`,
3,
map[string]int{"/api1": 20, "/api2": 30},
4 + 8,
http.StatusServiceUnavailable,
},
} {
t.Run(ti.name, func(t *testing.T) {
filterRegistry := builtin.MakeRegistry()
filterRegistry.Register(ratelimitfilters.NewBackendRatelimit())
ratelimitRegistry := ratelimit.NewSwarmRegistry(nil, &snet.RedisOptions{Addrs: []string{redisAddr}})
defer ratelimitRegistry.Close()
backends := newCountingBackends(ti.backends)
defer backends.close()
r := eskip.MustParse(strings.ReplaceAll(ti.routes, "$backends", backends.String()))
p := proxytest.WithParams(filterRegistry, proxy.Params{RateLimiters: ratelimitRegistry}, r...)
defer p.Close()
var urls []string
for path, count := range ti.requests {
for i := 0; i < count; i++ {
urls = append(urls, p.URL+path)
}
}
rand.Shuffle(len(urls), func(i, j int) { urls[i], urls[j] = urls[j], urls[i] })
for _, url := range urls {
rsp, err := http.Get(url)
if err != nil {
t.Fatalf("%s: %v", url, err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK && rsp.StatusCode != ti.rejectStatusCode {
t.Fatalf("%s: unexpected status %d", url, rsp.StatusCode)
}
}
for _, b := range backends {
if b.requests > ti.maxHits {
t.Errorf("Backend %s received %d above max hits %d ", b, b.requests, ti.maxHits)
}
}
})
}
}