fix: close request bodies when HTTP client rejects before transport
This commit is contained in:
@@ -88,11 +88,19 @@ func New(cfg Config) (*Client, error) {
|
|||||||
// consumed. Callers own response.Body. Only GET, HEAD and OPTIONS are retried,
|
// consumed. Callers own response.Body. Only GET, HEAD and OPTIONS are retried,
|
||||||
// and only when their bodies can be replayed. MaxRetries=0 disables retries.
|
// and only when their bodies can be replayed. MaxRetries=0 disables retries.
|
||||||
func (c *Client) Do(request *http.Request) (*http.Response, error) {
|
func (c *Client) Do(request *http.Request) (*http.Response, error) {
|
||||||
|
var requestError error
|
||||||
if c.closed.Load() {
|
if c.closed.Load() {
|
||||||
return nil, ErrClosed
|
requestError = ErrClosed
|
||||||
|
} else if request == nil || request.URL == nil {
|
||||||
|
requestError = errors.New("httpc: nil request or URL")
|
||||||
}
|
}
|
||||||
if request == nil || request.URL == nil {
|
if requestError != nil {
|
||||||
return nil, errors.New("httpc: nil request or URL")
|
// Match http.Client.Do's ownership rule even when rejection happens
|
||||||
|
// before the underlying transport takes ownership of the body.
|
||||||
|
if request != nil && request.Body != nil {
|
||||||
|
_ = request.Body.Close()
|
||||||
|
}
|
||||||
|
return nil, requestError
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(request.Context(), c.config.Timeout)
|
ctx, cancel := context.WithTimeout(request.Context(), c.config.Timeout)
|
||||||
base := request.Clone(ctx)
|
base := request.Clone(ctx)
|
||||||
|
|||||||
@@ -15,6 +15,31 @@ import (
|
|||||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type trackedBody struct {
|
||||||
|
io.Reader
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *trackedBody) Close() error { b.closed = true; return nil }
|
||||||
|
|
||||||
|
func TestRejectedRequestClosesBody(t *testing.T) {
|
||||||
|
for _, closed := range []bool{false, true} {
|
||||||
|
client, _ := New(Config{DisableLogging: true})
|
||||||
|
body := &trackedBody{Reader: strings.NewReader("body")}
|
||||||
|
request, _ := http.NewRequest("POST", "http://unused.invalid", body)
|
||||||
|
if closed {
|
||||||
|
client.Close()
|
||||||
|
} else {
|
||||||
|
request.URL = nil
|
||||||
|
}
|
||||||
|
_, err := client.Do(request)
|
||||||
|
client.Close()
|
||||||
|
if err == nil || !body.closed {
|
||||||
|
t.Fatalf("error=%v bodyClosed=%v", err, body.closed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRetryCorrelationPoolAndWriteSafety(t *testing.T) {
|
func TestRetryCorrelationPoolAndWriteSafety(t *testing.T) {
|
||||||
var calls, connections atomic.Int32
|
var calls, connections atomic.Int32
|
||||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user