Skip to content

Commit

Permalink
Merge pull request #32 from pritamsarkar007/master
Browse files Browse the repository at this point in the history
retry logic now supported for HTTP 429 code
  • Loading branch information
Seth Ammons authored Apr 25, 2018
2 parents ed9870d + cab6081 commit 38b020c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pester.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ func (c *Client) pester(p params) (*http.Response, error) {
}

// Early return if we have a valid result
// Only retry (ie, continue the loop) on 5xx status codes
if err == nil && resp.StatusCode < 500 {
// Only retry (ie, continue the loop) on 5xx status codes and 429

if err == nil && resp.StatusCode < 500 && resp.StatusCode != 429 {
multiplexCh <- result{resp: resp, err: err, req: n, retry: i}
return
}
Expand Down
123 changes: 123 additions & 0 deletions pester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,69 @@ func TestConcurrentRequests(t *testing.T) {
}
}

func TestConcurrentRequestsWith429(t *testing.T) {
t.Parallel()

c := New()
c.Concurrency = 3
c.KeepLog = true

port, err := serverWith429()
if err != nil {
t.Fatal("unable to start server", err)
}

url := fmt.Sprintf("http://localhost:%d", port)

response, err := c.Get(url)
if err != nil {
t.Fatal("unable to GET", err)
}
c.Wait()

response.Body.Close()
c.Wait()

// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := c.LogErrCount(), c.Concurrency*c.MaxRetries; got != want {
t.Errorf("got %d attempts, want %d", got, want)
}
}

func TestMaxRetriesConcurrentRequestsWith429(t *testing.T) {
t.Parallel()

c := New()
c.Concurrency = 3
c.KeepLog = true
c.MaxRetries = 5

port, err := serverWith429()
if err != nil {
t.Fatal("unable to start server", err)
}

url := fmt.Sprintf("http://localhost:%d", port)

response, err := c.Get(url)
if err != nil {
t.Fatal("unable to GET", err)
}
c.Wait()

response.Body.Close()
c.Wait()

// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := c.LogErrCount(), c.Concurrency*c.MaxRetries; got != want {
t.Errorf("got %d attempts, want %d", got, want)
}
}

func TestConcurrent2Retry0(t *testing.T) {
t.Parallel()

Expand All @@ -64,6 +127,36 @@ func TestConcurrent2Retry0(t *testing.T) {
}
}

func TestConcurrent2Retry0for429(t *testing.T) {
t.Parallel()

c := New()
c.Concurrency = 2
c.MaxRetries = 0
c.KeepLog = true

port, err := serverWith429()
if err != nil {
t.Fatal("unable to start server", err)
}

url := fmt.Sprintf("http://localhost:%d", port)

_, getErr := c.Get(url)

if getErr != nil {
t.Fatal("unable to GET", getErr)
}
c.Wait()

// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())

if got, want := c.LogErrCount(), c.Concurrency; got != want {
t.Errorf("got %d attempts, want %d", got, want)
}
}

func TestDefaultBackoff(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -506,3 +599,33 @@ func timeoutServer(timeout time.Duration) (int, error) {

return port, nil
}

func serverWith429() (int, error) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte("429 Too many requests"))
})
l, err := net.Listen("tcp", ":0")
if err != nil {
return -1, fmt.Errorf("unable to secure listener %v", err)
}
go func() {
if err := http.Serve(l, mux); err != nil {
log.Fatalf("slow-server error %v", err)
}
}()

var port int
_, sport, err := net.SplitHostPort(l.Addr().String())
if err == nil {
port, err = strconv.Atoi(sport)
}

if err != nil {
return -1, fmt.Errorf("unable to determine port %v", err)
}

return port, nil
}

0 comments on commit 38b020c

Please sign in to comment.