-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): enhance HTTP client with retry capability (#40)
Signed-off-by: Flc <[email protected]>
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
module github.com/addcnos/youdu/v2 | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 | ||
github.com/hashicorp/go-retryablehttp v0.7.7 | ||
github.com/stretchr/testify v1.10.0 | ||
) | ||
|
||
require ( | ||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,19 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= | ||
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= | ||
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= | ||
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= | ||
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU= | ||
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,57 @@ | ||
package youdu | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
var defaultHTTPClient = NewRetryableHTTPClient() | ||
|
||
type RetryableHTTPClientOption func(client *retryablehttp.Client) | ||
|
||
func WithRetryableHTTPClientLogger(logger retryablehttp.Logger) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.Logger = logger | ||
} | ||
} | ||
|
||
func WithRetryableHTTPClientRetryWaitMin(min time.Duration) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.RetryWaitMin = min | ||
} | ||
} | ||
|
||
func WithRetryableHTTPClientRetryWaitMax(max time.Duration) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.RetryWaitMax = max | ||
} | ||
} | ||
|
||
func WithRetryableHTTPClientRetryMax(max int) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.RetryMax = max | ||
} | ||
} | ||
|
||
func WithRetryableHTTPClientCheckRetry(checkRetry retryablehttp.CheckRetry) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.CheckRetry = checkRetry | ||
} | ||
} | ||
|
||
func WithRetryableHTTPClientBackoff(backoff retryablehttp.Backoff) RetryableHTTPClientOption { | ||
return func(client *retryablehttp.Client) { | ||
client.Backoff = backoff | ||
} | ||
} | ||
|
||
func NewRetryableHTTPClient(opts ...RetryableHTTPClientOption) *http.Client { | ||
retryClient := retryablehttp.NewClient() | ||
retryClient.Logger = nil | ||
for _, opt := range opts { | ||
opt(retryClient) | ||
} | ||
return retryClient.StandardClient() | ||
} |