generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support download with retry (#42)
* Add support download with retry * Fix the jcli download cmd * Add comments for export functions
- Loading branch information
1 parent
6c9a325
commit 7b271cd
Showing
7 changed files
with
104 additions
and
8 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
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,25 @@ | ||
package pkg | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// RetryClient is the wrap of http.Client | ||
type RetryClient struct { | ||
http.Client | ||
MaxAttempts int | ||
currentAttempts int | ||
} | ||
|
||
// Do is the wrap of http.Client.Do | ||
func (c *RetryClient) Do(req *http.Request) (rsp *http.Response, err error) { | ||
rsp, err = c.Client.Do(req) | ||
if _, ok := err.(*url.Error); ok { | ||
if c.currentAttempts < c.MaxAttempts { | ||
c.currentAttempts++ | ||
return c.Do(req) | ||
} | ||
} | ||
return | ||
} |
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,44 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/golang/mock/gomock" | ||
"github.com/linuxsuren/http-downloader/mock/mhttp" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
const fakeURL = "http://fake" | ||
|
||
func TestRetry(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
roundTripper := mhttp.NewMockRoundTripper(ctrl) | ||
|
||
client := &RetryClient{ | ||
Client: http.Client{ | ||
Transport: roundTripper, | ||
}, | ||
MaxAttempts: 3, | ||
} | ||
|
||
mockRequest, _ := http.NewRequest(http.MethodGet, fakeURL, nil) | ||
mockResponse := &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Proto: "HTTP/1.1", | ||
Request: mockRequest, | ||
Body: ioutil.NopCloser(bytes.NewBufferString("responseBody")), | ||
} | ||
roundTripper.EXPECT(). | ||
RoundTrip(mockRequest).Return(mockResponse, nil) | ||
|
||
request, _ := http.NewRequest(http.MethodGet, fakeURL, nil) | ||
response, err := client.Do(request) | ||
fmt.Println(reflect.TypeOf(err)) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
} |