-
Notifications
You must be signed in to change notification settings - Fork 28
Add retry mechanism to 502 response (AST-82566) #1025
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e9c8a9
Add Retry
tiagobcx f8bb246
Retry for 4 times. and add UT
tiagobcx 9dd0186
Close resp and fix retry func
tiagobcx e803233
Merge branch 'main' into feature/saraChen/AddRetryToHttpCall
cx-sarah-chen da08bdc
Fix comment
tiagobcx 7427c85
Fix comment
tiagobcx 4216470
Fix comment
tiagobcx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,80 @@ | ||
| package wrappers | ||
|
|
||
| import ( | ||
| "errors" | ||
| "github.com/stretchr/testify/assert" | ||
| "net/http" | ||
| "testing" | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "time" | ||
| ) | ||
|
|
||
| type mockReadCloser struct{} | ||
|
|
||
| func (m *mockReadCloser) Read(p []byte) (n int, err error) { | ||
| return 0, nil | ||
| } | ||
|
|
||
| func (m *mockReadCloser) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func TestRetryHTTPRequest_Success(t *testing.T) { | ||
| fn := func() (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: &mockReadCloser{}, | ||
| }, nil | ||
| } | ||
|
|
||
| resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond) | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.NoError(t, err) | ||
| assert.NotNil(t, resp) | ||
| assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestRetryHTTPRequest_RetryOnBadGateway(t *testing.T) { | ||
| attempts := 0 | ||
| fn := func() (*http.Response, error) { | ||
| attempts++ | ||
| if attempts < retryAttempts { | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return &http.Response{ | ||
| StatusCode: http.StatusBadGateway, | ||
| Body: &mockReadCloser{}, | ||
| }, nil | ||
| } | ||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: &mockReadCloser{}, | ||
| }, nil | ||
| } | ||
|
|
||
| resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond) | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.NoError(t, err) | ||
| assert.NotNil(t, resp) | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
| assert.Equal(t, retryAttempts, attempts) | ||
| } | ||
|
|
||
| func TestRetryHTTPRequest_Fail(t *testing.T) { | ||
| fn := func() (*http.Response, error) { | ||
| return nil, errors.New("network error") | ||
| } | ||
|
|
||
| resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond) | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.Error(t, err) | ||
| assert.Nil(t, resp) | ||
| } | ||
|
|
||
| func TestRetryHTTPRequest_EndWithBadGateway(t *testing.T) { | ||
| fn := func() (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: http.StatusBadGateway, | ||
| Body: &mockReadCloser{}, | ||
| }, nil | ||
| } | ||
|
|
||
| resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond) | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.NoError(t, err) | ||
| assert.NotNil(t, resp) | ||
| assert.Equal(t, http.StatusBadGateway, resp.StatusCode) | ||
| } | ||
This file contains hidden or 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,7 @@ | ||
| package wrappers | ||
|
|
||
| const ( | ||
| limitValue = "10000" | ||
| retryAttempts = 4 | ||
| retryDelay = 500 | ||
cx-sarah-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.