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

receive "x-ms-retry-after-ms" field on error 429 #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io"
"net/http"
"strconv"
"time"
)

type Clienter interface {
Expand Down Expand Up @@ -146,7 +148,18 @@ func (c *Client) do(r *Request, validator statusCodeValidatorFunc, data interfac
return nil, err
}
if !validator(resp.StatusCode) {
err = &RequestError{}
if resp.StatusCode == 429 {
retry := resp.Header.Get("x-ms-retry-after-ms")
val, _ := strconv.Atoi(retry)
if val == 0 {
val = 100
}
err = &RequestError429{
Retry: time.Duration(val) * time.Millisecond,
}
} else {
err = &RequestError{}
}
readJson(resp.Body, &err)
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func (e RequestError) Error() string {
return fmt.Sprintf("%v, %v", e.Code, e.Message)
}

// Request Error
type RequestError429 struct {
Code string `json:"code"`
Message string `json:"message"`
Retry time.Duration
}

// Implement Error function
func (e RequestError429) Error() string {
return fmt.Sprintf("%v, retry after %v", e.Code, e.Retry)
}

// Resource Request
type Request struct {
rId, rType string
Expand Down