Skip to content

Commit

Permalink
Dev (#2)
Browse files Browse the repository at this point in the history
* fix(client): Attempt index doesn't need +1

* refactor(client): reduce default RetryMax (5 -> 3)

* chore(mode): go get -f -t -u ./...

* refactor(request): hq-go-url parsing for requests

* refactor(request): use parser with default scheme
  • Loading branch information
enenumxela authored Feb 12, 2025
1 parent 66ca30c commit aa2cc99
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *Client) Do(req *request.Request) (res *http.Response, err error) {
if c.onError != nil {
c.closeIdleConnections()

res, err = c.onError(res, err, c.cfg.RetryMax+1)
res, err = c.onError(res, err, c.cfg.RetryMax)

return
}
Expand All @@ -130,9 +130,9 @@ func (c *Client) Do(req *request.Request) (res *http.Response, err error) {
if res != nil {
res.Body.Close()

err = fmt.Errorf("%s %s giving up after %d attempts: response status %d: %w", req.Method, req.URL, c.cfg.RetryMax+1, res.StatusCode, err)
err = fmt.Errorf("%s %s giving up after %d attempts: response status %d: %w", req.Method, req.URL, c.cfg.RetryMax, res.StatusCode, err)
} else {
err = fmt.Errorf("%s %s giving up after %d attempts: %w", req.Method, req.URL, c.cfg.RetryMax+1, err)
err = fmt.Errorf("%s %s giving up after %d attempts: %w", req.Method, req.URL, c.cfg.RetryMax, err)
}

c.closeIdleConnections()
Expand Down Expand Up @@ -446,7 +446,7 @@ var (
// It is intended for standard scenarios where connection pooling is acceptable.
var DefaultSingleClientConfiguration = &ClientConfiguration{
Timeout: 30 * time.Second,
RetryMax: 5,
RetryMax: 3,
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 30 * time.Second,
KillIdleConn: false,
Expand All @@ -457,7 +457,7 @@ var DefaultSingleClientConfiguration = &ClientConfiguration{
// where killing idle connections is desirable to reduce resource usage.
var DefaultSprayingClientConfiguration = &ClientConfiguration{
Timeout: 30 * time.Second,
RetryMax: 5,
RetryMax: 3,
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 30 * time.Second,
KillIdleConn: true,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ go 1.23.3

require (
github.com/stretchr/testify v1.10.0
go.source.hueristiq.com/retrier v0.0.0-20250211115025-9185c5476b26
go.source.hueristiq.com/retrier v0.0.0-20250211140259-af5f5f3556b7
go.source.hueristiq.com/url v0.0.0-20250210155042-fe75d4c63dad
golang.org/x/net v0.35.0
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.source.hueristiq.com/retrier v0.0.0-20250211115025-9185c5476b26 h1:mSgOREgLzLXBZbkgyr5yFi9hmkeQLBKM2RTIzMiwk88=
go.source.hueristiq.com/retrier v0.0.0-20250211115025-9185c5476b26/go.mod h1:ohGUnbF/yXWsCKrOb4J6JrSJGukUTBgLBDIir1oNBA4=
go.source.hueristiq.com/retrier v0.0.0-20250211140259-af5f5f3556b7 h1:xP1+QfUB17vmnCe5qJ1R2uBxBiJD1JKgCCxSuK36dWg=
go.source.hueristiq.com/retrier v0.0.0-20250211140259-af5f5f3556b7/go.mod h1:ohGUnbF/yXWsCKrOb4J6JrSJGukUTBgLBDIir1oNBA4=
go.source.hueristiq.com/url v0.0.0-20250210155042-fe75d4c63dad h1:1Vbx0oEvSS8oebJBbbRrvxPtwQrgqWtgwV+vJ4b7TEI=
go.source.hueristiq.com/url v0.0.0-20250210155042-fe75d4c63dad/go.mod h1:AHuHYgNJAvUCk70o7Fm3GFlXZGGTrAL4L7APZZiKyHk=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
Expand Down
17 changes: 16 additions & 1 deletion request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"io"
"net/http"

"go.source.hueristiq.com/url/parser"
)

// Request is a wrapper around http.Request that enables the request body to be reusable.
Expand Down Expand Up @@ -99,11 +101,24 @@ func NewFromURL(method, URL string, body interface{}) (req *Request, err error)
// - req (*Request): A pointer to the newly created Request wrapper containing an http.Request.
// - err (error): An error value if the request creation fails (for example, due to an unsupported body type).
func NewFromURLWithContext(ctx context.Context, method, URL string, body interface{}) (req *Request, err error) {
internalHTTPRequest, err := http.NewRequestWithContext(ctx, method, URL, nil) //nolint:gocritic // To be refactored
parsedURL, err := parser.NewURLParser(parser.URLParserWithDefaultScheme("http")).Parse(URL)
if err != nil {
return
}

// we provide a url without path to http.NewRequest at start and then replace url instance directly
// because `http.NewRequest()` internally parses using `url.Parse()` this removes/overrides any
// patches done by parsed.URL in unsafe mode (ex: https://example.com/%invalid)
//
// Note: this does not have any impact on actual path when sending request
// `http.NewRequestxxx` internally only uses `u.Host` and all other data is stored in `url.URL` instance
internalHTTPRequest, err := http.NewRequestWithContext(ctx, method, parsedURL.Scheme+"://"+parsedURL.Host, nil) //nolint:gocritic // To be refactored
if err != nil {
return
}

internalHTTPRequest.URL = parsedURL.URL

reusableBodyReadCloser, err := getReusableBodyReadCloser(body)
if err != nil {
return
Expand Down

0 comments on commit aa2cc99

Please sign in to comment.