Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨feat: add retryIf function to client agent #1726

Merged
merged 1 commit into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions client.go
Expand Up @@ -48,6 +48,11 @@ type Response = fasthttp.Response
// Copy from fasthttp
type Args = fasthttp.Args

// RetryIfFunc signature of retry if function
// Request argument passed to RetryIfFunc, if there are any request errors.
// Copy from fasthttp
type RetryIfFunc = fasthttp.RetryIfFunc

var defaultClient Client

// Client implements http client.
Expand Down Expand Up @@ -718,6 +723,14 @@ func (a *Agent) Dest(dest []byte) *Agent {
return a
}

// RetryIf controls whether a retry should be attempted after an error.
//
// By default, will use isIdempotent function from fasthttp
func (a *Agent) RetryIf(retryIf RetryIfFunc) *Agent {
a.HostClient.RetryIf = retryIf
return a
}

/************************** End Agent Setting **************************/
var warnOnce sync.Once

Expand Down
61 changes: 61 additions & 0 deletions client_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
stdjson "encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
Expand Down Expand Up @@ -562,6 +563,66 @@ func Test_Client_Agent_Dest(t *testing.T) {
})
}

// readErrorConn is a struct for testing retryIf
type readErrorConn struct {
net.Conn
}

func (r *readErrorConn) Read(p []byte) (int, error) {
return 0, fmt.Errorf("error")
}

func (r *readErrorConn) Write(p []byte) (int, error) {
return len(p), nil
}

func (r *readErrorConn) Close() error {
return nil
}

func (r *readErrorConn) LocalAddr() net.Addr {
return nil
}

func (r *readErrorConn) RemoteAddr() net.Addr {
return nil
}
func Test_Client_Agent_RetryIf(t *testing.T) {
t.Parallel()

ln := fasthttputil.NewInmemoryListener()

app := New(Config{DisableStartupMessage: true})

go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()

a := Post("http://example.com").
RetryIf(func(req *Request) bool {
return true
})
dialsCount := 0
a.HostClient.Dial = func(addr string) (net.Conn, error) {
dialsCount++
switch dialsCount {
case 1:
return &readErrorConn{}, nil
case 2:
return &readErrorConn{}, nil
case 3:
return &readErrorConn{}, nil
case 4:
return ln.Dial()
default:
t.Fatalf("unexpected number of dials: %d", dialsCount)
}
panic("unreachable")
}

_, _, errs := a.String()
utils.AssertEqual(t, dialsCount, 4)
utils.AssertEqual(t, 0, len(errs))
}

func Test_Client_Stdjson_Gojson(t *testing.T) {
type User struct {
Account *string `json:"account"`
Expand Down