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

Add a Retry-After header when rate limit is exceeded #3704

Merged
merged 1 commit into from
Jan 23, 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
1 change: 1 addition & 0 deletions pkg/apiserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
return
}
w.WriteHeader(http.StatusServiceUnavailable)
w.Header().Set("Retry-After", "1")
fmt.Fprintf(w, "Rate limit exceeded.")
})
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
"github.com/golang/glog"
)

// specialParams lists parameters that are handled specially and which users of Request
Expand Down Expand Up @@ -457,6 +458,10 @@ func (r *Request) Do() Result {
client = http.DefaultClient
}

// Right now we make about ten retry attempts if we get a Retry-After response.
// TODO: Change to a timeout based approach.
retries := 0

for {
if r.err != nil {
return Result{err: &RequestConstructionError{r.err}}
Expand All @@ -478,6 +483,19 @@ func (r *Request) Do() Result {
continue
}

if resp.StatusCode == http.StatusServiceUnavailable {
if retries < 10 {
retries++
if waitFor := resp.Header.Get("Retry-After"); waitFor != "" {
delay, err := strconv.Atoi(waitFor)
if err == nil {
glog.V(4).Infof("Got a Retry-After %s response for attempt %d to %v", waitFor, retries, r.finalURL())
time.Sleep(time.Duration(delay) * time.Second)
continue
}
}
}
}
return Result{respBody, created, err, r.codec}
}
}
Expand Down