Skip to content

Too many retries leads to... success #43

@denysvitali

Description

@denysvitali

When a RetryBackOffErrFn is defined and the call fails all the time, the error being returned by req.Run might be nil, indicating a success.

This only happens when there aren't network errors. An user might still want to define their RetryBackOffErrFn as follows:

package httpc_test
 
import (
	"net/http"
	"testing"
	"time"
 
	"github.com/fako1024/httpc"
	"github.com/h2non/gock"
)
 
func TestHttpcRetry(t *testing.T) {
	gock.Observe(func(request *http.Request, _ gock.Mock) {
		t.Logf("%s %s\n", request.Method, request.URL)
	})
	gock.New("https://example.com").
		Persist().
		Get("/").
		Reply(http.StatusInternalServerError)
 
	err := httpc.New(http.MethodGet, "https://example.com").
		ModifyHTTPClient(func(c *http.Client) {
			c.Transport = gock.DefaultTransport
		}).
		RetryBackOffErrFn(func(resp *http.Response, err error) bool {
			if resp == nil || err != nil {
				return true
			}
			t.Logf("status code: %d", resp.StatusCode)
			if resp.StatusCode >= 200 && resp.StatusCode < 300 {
				return false
			}
			return true
		}).
		RetryBackOff(httpc.Intervals{time.Millisecond, time.Millisecond, time.Millisecond}).
		Run()
	if err == nil {
		t.Error("expected error")
	}
}

This results in:

=== RUN   TestHttpcRetry
    httpc_test.go:14: GET https://example.com
    httpc_test.go:29: status code: 500
    httpc_test.go:14: GET https://example.com
    httpc_test.go:29: status code: 500
    httpc_test.go:14: GET https://example.com
    httpc_test.go:29: status code: 500
    httpc_test.go:14: GET https://example.com
    httpc_test.go:29: status code: 500
    httpc_test.go:29: status code: 500
    httpc_test.go:38: expected error
--- FAIL: TestHttpcRetry (0.00s)

I would expect the err to not be nil since the call never succeeded (based on the definition of the retrial mechanism).

It seems like this bug affects only calls that do not cause an err (network errors).

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions