Skip to content

Instantly share code, notes, and snippets.

@jacoelho
Created July 30, 2017 23:02
Show Gist options
  • Select an option

  • Save jacoelho/5132f180c1a4acc46c62dd988c7ffe1f to your computer and use it in GitHub Desktop.

Select an option

Save jacoelho/5132f180c1a4acc46c62dd988c7ffe1f to your computer and use it in GitHub Desktop.
retry golang
type Retry struct {
http.RoundTripper
}
func (r *Retry) RoundTrip(req *http.Request) (*http.Response, error) {
for {
log.Printf("retry: doing request %s\n", req.URL)
resp, err := r.RoundTripper.RoundTrip(req)
if err == nil && resp.StatusCode < 500 {
return resp, err
}
// discard
if resp != nil {
resp.Body.Close()
}
select {
case <-req.Context().Done():
return resp, req.Context().Err()
case <-time.After(2 * time.Second):
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment