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

Switch to use Too Many Requests response code #3781

Merged
merged 1 commit into from
Jan 28, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/api/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
)

// HTTP Status codes not in the golang http package.
const (
StatusUnprocessableEntity = 422
StatusTooManyRequests = 429
)

// StatusError is an error intended for consumption by a REST API server; it can also be
// reconstructed by clients from a REST response. Public to allow easy type switches.
type StatusError struct {
Expand Down Expand Up @@ -134,7 +140,7 @@ func NewInvalid(kind, name string, errs ValidationErrorList) error {
}
return &StatusError{api.Status{
Status: api.StatusFailure,
Code: 422, // RFC 4918: StatusUnprocessableEntity
Code: StatusUnprocessableEntity, // RFC 4918: StatusUnprocessableEntity
Reason: api.StatusReasonInvalid,
Details: &api.StatusDetails{
Kind: kind,
Expand Down
4 changes: 0 additions & 4 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ type defaultAPIServer struct {
group *APIGroupVersion
}

const (
StatusUnprocessableEntity = 422
)

// Handle returns a Handler function that exposes the provided storage interfaces
// as RESTful resources at prefix, serialized by codec, and also includes the support
// http resources.
Expand Down
8 changes: 5 additions & 3 deletions pkg/apiserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/authorizer"
authhandlers "github.com/GoogleCloudPlatform/kubernetes/pkg/auth/handlers"
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
Expand Down Expand Up @@ -71,9 +72,10 @@ func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
handler.ServeHTTP(w, req)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
// Return a 429 status indicating "Too Many Requests"
w.WriteHeader(errors.StatusTooManyRequests)
w.Header().Set("Retry-After", "1")
fmt.Fprintf(w, "Rate limit exceeded.")
fmt.Fprintf(w, "Rate limit is 1 QPS or a burst of 20")
})
}

Expand All @@ -96,7 +98,7 @@ func RecoverPanics(handler http.Handler) http.Handler {
http.StatusTemporaryRedirect,
http.StatusConflict,
http.StatusNotFound,
StatusUnprocessableEntity,
errors.StatusUnprocessableEntity,
),
).Log()

Expand Down
3 changes: 2 additions & 1 deletion pkg/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ func (r *Request) Do() Result {
continue
}

if resp.StatusCode == http.StatusServiceUnavailable {
// Check to see if we got a 429 Too Many Requests response code.
if resp.StatusCode == errors.StatusTooManyRequests {
if retries < 10 {
retries++
if waitFor := resp.Header.Get("Retry-After"); waitFor != "" {
Expand Down