-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement retry on HTTP error for broken connection
- Loading branch information
Showing
14 changed files
with
355 additions
and
121 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package retryhttp | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
|
||
"github.com/cash-track/gateway/http" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
const defaultRetryAttempts = 1 | ||
|
||
type Client interface { | ||
http.Client | ||
WithRetryAttempts(attempts uint) Client | ||
DoWithRetry(req *fasthttp.Request, resp *fasthttp.Response, attempts uint) error | ||
} | ||
|
||
type FastHttpRetryClient struct { | ||
http.Client | ||
attempts uint | ||
} | ||
|
||
func NewFastHttpRetryClient() Client { | ||
return &FastHttpRetryClient{ | ||
Client: http.NewFastHttpClient(), | ||
attempts: defaultRetryAttempts, | ||
} | ||
} | ||
|
||
func (c *FastHttpRetryClient) Do(req *fasthttp.Request, resp *fasthttp.Response) error { | ||
return c.DoWithRetry(req, resp, c.attempts) | ||
} | ||
|
||
func (c *FastHttpRetryClient) DoWithRetry(req *fasthttp.Request, resp *fasthttp.Response, attempts uint) error { | ||
err := c.Client.Do(req, resp) | ||
|
||
if attempts == 1 || err == nil || !strings.Contains(err.Error(), "broken pipe") { | ||
return err | ||
} | ||
|
||
log.Printf("retrying request due to an error [attempt %d] : %s", attempts, err.Error()) | ||
|
||
return c.DoWithRetry(req, resp, attempts-1) | ||
} | ||
|
||
func (c *FastHttpRetryClient) WithRetryAttempts(attempts uint) Client { | ||
c.attempts = attempts | ||
return c | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package retryhttp | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/valyala/fasthttp" | ||
"go.uber.org/mock/gomock" | ||
|
||
"github.com/cash-track/gateway/http" | ||
httpmock "github.com/cash-track/gateway/mocks/http" | ||
) | ||
|
||
func TestNewFastHttpRetryClient(t *testing.T) { | ||
client := NewFastHttpRetryClient() | ||
assert.NotNil(t, client) | ||
} | ||
|
||
func TestDoWithRetry(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
c := httpmock.NewClientMock(ctrl) | ||
c.EXPECT().Do(gomock.Any(), gomock.Any()).Times(2).Return(fmt.Errorf("unknown error: broken pipe or closed connection")) | ||
|
||
client := FastHttpRetryClient{ | ||
Client: c, | ||
} | ||
|
||
client.WithRetryAttempts(2) | ||
err := client.Do(&fasthttp.Request{}, &fasthttp.Response{}) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestWithRetryAttempts(t *testing.T) { | ||
client := FastHttpRetryClient{ | ||
Client: &http.FastHttpClient{}, | ||
} | ||
client.WithRetryAttempts(3) | ||
|
||
assert.Equal(t, uint(3), client.attempts) | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.