-
Notifications
You must be signed in to change notification settings - Fork 32
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
Retry with backoff when rate-limited #35
Changes from 2 commits
da4a536
176c52c
3382225
d782d8b
8db66c1
4a8c894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package heroku | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cenkalti/backoff" | ||
) | ||
|
||
func RoundTripWithRetryBackoff(req *http.Request) (*http.Response, error) { | ||
var lastResponse *http.Response | ||
var lastError error | ||
|
||
retryableRoundTrip := func() error { | ||
lastResponse, lastError = http.DefaultTransport.RoundTrip(req) | ||
// Detect Heroku API rate limiting | ||
// https://devcenter.heroku.com/articles/platform-api-reference#client-error-responses | ||
if lastResponse.StatusCode == 429 { | ||
return fmt.Errorf("Heroku API rate limited: 429 Too Many Requests") | ||
} | ||
return nil | ||
} | ||
|
||
err := backoff.Retry(retryableRoundTrip, backoff.NewExponentialBackOff()) | ||
// Propagate the rate limit error when retries eventually fail. | ||
if err != nil { | ||
if lastResponse != nil { | ||
lastResponse.Body.Close() | ||
} | ||
return nil, err | ||
} | ||
// Propagate all other response errors. | ||
if lastError != nil { | ||
if lastResponse != nil { | ||
lastResponse.Body.Close() | ||
} | ||
return nil, lastError | ||
} | ||
|
||
return lastResponse, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably need to check lastError and return it if not nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lastError
is checked just a few lines down in the parent function, because thisbackoff.Retry
function should only return an error when it should be retried (status 429 for this rate-limiting use-case).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could dry it up by dropping
lastError
and doing something like this:That would also mean that you would only need to handle the error once below...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we only want this to retry for HTTP status 429 responses.
If that function returns other errors, it will retry for them too.
We only want to retry when rate-limited, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah 😊