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
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
The code in this file is adapted from MIT-licensed code in github.com/go-chi/chi. Thanks chi (thi)!
See: https://github.com/go-chi/chi/blob/e6baba61759b26ddf7b14d1e02d1da81a4d76c08/middleware/throttle.go
And: https://github.com/sponsors/pkieltyka
*/
package middleware_test
import (
"context"
"net/http"
"net/http/httptest"
"runtime"
"strconv"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/middleware"
)
func TestThrottlingMiddleware(t *testing.T) {
testThrottlingMiddleware(t, 2, time.Second*10)
testThrottlingMiddleware(t, 4, time.Second*15)
testThrottlingMiddleware(t, 8, time.Second*30)
}
func testThrottlingMiddleware(t *testing.T, cpuMulti int, retryAfter time.Duration) {
// Calculate expected request limit + queue.
limit := runtime.GOMAXPROCS(0) * cpuMulti
queueLimit := limit * cpuMulti
// Calculate expected retry-after header string.
retryAfterStr := strconv.FormatUint(uint64(retryAfter/time.Second), 10)
// Gin test http engine
// (used for ctx init).
e := gin.New()
// Add middleware to the gin engine handler stack.
middleware := middleware.Throttle(cpuMulti, retryAfter)
e.Use(middleware)
// Set the blocking gin handler.
handler := blockingHandler()
e.Handle("GET", "/", handler)
var cncls []func()
for i := 0; i < queueLimit+limit; i++ {
// Prepare a gin test context.
r := httptest.NewRequest("GET", "/", nil)
rw := httptest.NewRecorder()
// Wrap request with new cancel context.
ctx, cncl := context.WithCancel(r.Context())
r = r.WithContext(ctx)
// Pass req through
// engine handler.
go e.ServeHTTP(rw, r)
time.Sleep(time.Millisecond)
// Get http result.
res := rw.Result()
if i < queueLimit {
// Check status == 200 (default, i.e not set).
if res.StatusCode != http.StatusOK {
t.Fatalf("status code was set (%d) with queueLimit=%d and request=%d", res.StatusCode, queueLimit, i)
}
// Add cancel to func slice.
cncls = append(cncls, cncl)
} else {
// Check the returned status code is expected.
if res.StatusCode != http.StatusTooManyRequests {
t.Fatalf("did not return status 429 (%d) with queueLimit=%d and request=%d", res.StatusCode, queueLimit, i)
}
// Check the returned retry-after header is set.
if res.Header.Get("Retry-After") != retryAfterStr {
t.Fatalf("did not return retry-after %s with queueLimit=%d and request=%d", retryAfterStr, queueLimit, i)
}
// Cancel on return.
defer cncl()
}
}
// Cancel all blocked reqs.
for _, cncl := range cncls {
cncl()
}
time.Sleep(time.Second)
// Check a bunchh more requests
// can now make it through after
// previous requests were released!
for i := 0; i < limit; i++ {
// Prepare a gin test context.
r := httptest.NewRequest("GET", "/", nil)
rw := httptest.NewRecorder()
// Pass req through
// engine handler.
go e.ServeHTTP(rw, r)
time.Sleep(5 * time.Millisecond)
// Get http result.
res := rw.Result()
// Check status == 200 (default, i.e not set).
if res.StatusCode != http.StatusOK {
t.Fatalf("status code was set (%d) with queueLimit=%d and request=%d", res.StatusCode, queueLimit, i)
}
}
}
func blockingHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
<-ctx.Done()
ctx.Status(201) // specifically not 200
}
}
|