Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respond with 429 when throttled #528

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions middleware/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.Handler {

case <-ctx.Done():
t.setRetryAfterHeaderIfNeeded(w, true)
http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
http.Error(w, errContextCanceled, http.StatusTooManyRequests)
return

case btok := <-t.backlogTokens:
Expand All @@ -85,12 +85,12 @@ func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.Handler {
select {
case <-timer.C:
t.setRetryAfterHeaderIfNeeded(w, false)
http.Error(w, errTimedOut, http.StatusServiceUnavailable)
http.Error(w, errTimedOut, http.StatusTooManyRequests)
return
case <-ctx.Done():
timer.Stop()
t.setRetryAfterHeaderIfNeeded(w, true)
http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
http.Error(w, errContextCanceled, http.StatusTooManyRequests)
return
case tok := <-t.tokens:
defer func() {
Expand All @@ -103,7 +103,7 @@ func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.Handler {

default:
t.setRetryAfterHeaderIfNeeded(w, false)
http.Error(w, errCapacityExceeded, http.StatusServiceUnavailable)
http.Error(w, errCapacityExceeded, http.StatusTooManyRequests)
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions middleware/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {

buf, err := ioutil.ReadAll(res.Body)
assertNoError(t, err)
assertEqual(t, http.StatusServiceUnavailable, res.StatusCode)
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
assertEqual(t, errTimedOut, strings.TrimSpace(string(buf)))

}(i)
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestThrottleMaximum(t *testing.T) {

buf, err := ioutil.ReadAll(res.Body)
assertNoError(t, err)
assertEqual(t, http.StatusServiceUnavailable, res.StatusCode)
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
assertEqual(t, errCapacityExceeded, strings.TrimSpace(string(buf)))

}(i)
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestThrottleRetryAfter(t *testing.T) {

res, err := client.Get(server.URL)
assertNoError(t, err)
assertEqual(t, http.StatusServiceUnavailable, res.StatusCode)
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
assertEqual(t, res.Header.Get("Retry-After"), "3600")
}(i)
}
Expand Down