Skip to content

Instantly share code, notes, and snippets.

@mwhooker
Created November 2, 2013 05:56
Show Gist options
  • Save mwhooker/7276016 to your computer and use it in GitHub Desktop.
Save mwhooker/7276016 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/mwhooker/go.httpcontrol"
"net"
"net/http"
"time"
)
func awsRetry(req *http.Request, res *http.Response, err error) bool {
retry := false
if err == nil && res != nil {
retry = false
}
if neterr, ok := err.(net.Error); ok {
if neterr.Temporary() {
retry = true
}
}
if res != nil {
if 500 > res.StatusCode && res.StatusCode >= 400 {
retry = true
}
}
fmt.Println("retry", req.URL, retry)
return retry
}
func main() {
var c = &http.Client{
Transport: &httpcontrol.Transport{
ShouldRetry: awsRetry,
RequestTimeout: time.Second * 2,
DialTimeout: time.Second * 2,
MaxTries: 3,
},
}
fmt.Println(c.Get("http://httpbin.org/delay/0"))
resp, err := c.Get("http://httpbin.org/status/418")
if err == nil {
fmt.Println(resp, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment