Created
July 30, 2017 23:02
-
-
Save jacoelho/5132f180c1a4acc46c62dd988c7ffe1f to your computer and use it in GitHub Desktop.
retry golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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