Skip to content

Commit abf52fe

Browse files
jlucktaymigueleliasweb
authored andcommitted
refactor: split out middleware for later re-use
Also expose the burst parameter offered by `rate.NewLimiter`.
1 parent dd18230 commit abf52fe

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/mock/server_options.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ func WithRequestMatchPagesEnterprise(
134134
return WithRequestMatchPages(ep, pages...)
135135
}
136136

137-
// WithRateLimit enforces a rate limit using [golang.org/x/time/rate].
137+
// rateLimitMiddleware enforces a rate limit using [golang.org/x/time/rate].
138138
// rps is the number of requests per second allowed by the rate limiter.
139-
func WithRateLimit(rps float64) MockBackendOption {
140-
limiter := rate.NewLimiter(rate.Limit(rps), 1)
139+
// Higher burst values allow more calls to happen at once.
140+
// A zero value for burst will not allow any events, unless rps == [rate.Inf].
141+
// This middleware is intended to be used with [github.com/gorilla/mux].
142+
func rateLimitMiddleware(rps float64, burst int) func(next http.Handler) http.Handler {
143+
limiter := rate.NewLimiter(rate.Limit(rps), burst)
141144

142-
rateLimitMiddleware := func(next http.Handler) http.Handler {
145+
return func(next http.Handler) http.Handler {
143146
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
144147
if !limiter.Allow() {
145148
// These values are based on this bit of logic within [github.com/google/go-github]:
@@ -153,8 +156,11 @@ func WithRateLimit(rps float64) MockBackendOption {
153156
next.ServeHTTP(w, r)
154157
})
155158
}
159+
}
156160

161+
// WithRateLimit enforces a rate limit on the mocked [http.Client].
162+
func WithRateLimit(rps float64, burst int) MockBackendOption {
157163
return func(router *mux.Router) {
158-
router.Use(rateLimitMiddleware)
164+
router.Use(rateLimitMiddleware(rps, burst))
159165
}
160166
}

src/mock/server_options_external_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ func TestWithRateLimit(t *testing.T) {
2525
[]github.Repository{{Name: github.String(repoTwo)}},
2626
),
2727

28-
// The rate limiter will allow 10 requests per second.
29-
mock.WithRateLimit(10),
28+
// The rate limiter will allow 10 requests per second, and a burst size of 1.
29+
// These two options together mean that the rate of requests will be strictly enforced, so if any two requests are
30+
// made less than 1/10th of a second apart, the latter will be refused and come back with a rate limit error.
31+
mock.WithRateLimit(10, 1),
3032
)
3133

3234
ghc := github.NewClient(mhc)
@@ -45,7 +47,8 @@ func TestWithRateLimit(t *testing.T) {
4547

4648
rleCount++
4749

48-
// Sleeping for one tenth of a second should be enough for the rate limiter on the mock server.
50+
// After hitting the rate limiter and getting the appropriate error above, sleeping for one tenth of a second
51+
// should be enough to reset the limiter and try requesting the repo list again.
4952
time.Sleep(time.Second / 10)
5053

5154
continue

0 commit comments

Comments
 (0)