fix: drain the req.body into io.Discard correctly (#17881)

This commit is contained in:
jiuker 2023-08-21 16:09:07 +08:00 committed by GitHub
parent e3ea97c964
commit fa2a8d7209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 17 deletions

View File

@ -23,7 +23,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -475,12 +474,10 @@ func (c *esClientV7) createIndex(args ElasticsearchArgs) error {
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close() defer DrainBody(resp.Body)
if resp.IsError() { if resp.IsError() {
err := fmt.Errorf("Create index err: %s", res.String()) return fmt.Errorf("Create index err: %v", res)
return err
} }
io.Copy(io.Discard, resp.Body)
return nil return nil
} }
return nil return nil
@ -493,8 +490,7 @@ func (c *esClientV7) ping(ctx context.Context, _ ElasticsearchArgs) (bool, error
if err != nil { if err != nil {
return false, store.ErrNotConnected return false, store.ErrNotConnected
} }
io.Copy(io.Discard, resp.Body) DrainBody(resp.Body)
resp.Body.Close()
return !resp.IsError(), nil return !resp.IsError(), nil
} }
@ -507,8 +503,7 @@ func (c *esClientV7) entryExists(ctx context.Context, index string, key string)
if err != nil { if err != nil {
return false, err return false, err
} }
io.Copy(io.Discard, res.Body) DrainBody(res.Body)
res.Body.Close()
return !res.IsError(), nil return !res.IsError(), nil
} }
@ -523,8 +518,7 @@ func (c *esClientV7) removeEntry(ctx context.Context, index string, key string)
if err != nil { if err != nil {
return err return err
} }
defer res.Body.Close() defer DrainBody(res.Body)
defer io.Copy(io.Discard, res.Body)
if res.IsError() { if res.IsError() {
return fmt.Errorf("Delete err: %s", res.String()) return fmt.Errorf("Delete err: %s", res.String())
} }
@ -552,8 +546,7 @@ func (c *esClientV7) updateEntry(ctx context.Context, index string, key string,
if err != nil { if err != nil {
return err return err
} }
defer res.Body.Close() defer DrainBody(res.Body)
defer io.Copy(io.Discard, res.Body)
if res.IsError() { if res.IsError() {
return fmt.Errorf("Update err: %s", res.String()) return fmt.Errorf("Update err: %s", res.String())
} }
@ -579,8 +572,7 @@ func (c *esClientV7) addEntry(ctx context.Context, index string, eventData event
if err != nil { if err != nil {
return err return err
} }
defer res.Body.Close() defer DrainBody(res.Body)
defer io.Copy(io.Discard, res.Body)
if res.IsError() { if res.IsError() {
return fmt.Errorf("Add err: %s", res.String()) return fmt.Errorf("Add err: %s", res.String())
} }

View File

@ -34,6 +34,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/dustin/go-humanize"
"github.com/minio/minio/internal/event" "github.com/minio/minio/internal/event"
"github.com/minio/minio/internal/logger" "github.com/minio/minio/internal/logger"
"github.com/minio/minio/internal/once" "github.com/minio/minio/internal/once"
@ -194,8 +195,7 @@ func (target *WebhookTarget) send(eventData event.Event) error {
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close() defer DrainBody(resp.Body)
io.Copy(io.Discard, resp.Body)
if resp.StatusCode < 200 || resp.StatusCode > 299 { if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("sending event failed with %v", resp.Status) return fmt.Errorf("sending event failed with %v", resp.Status)
@ -297,3 +297,23 @@ func NewWebhookTarget(ctx context.Context, id string, args WebhookArgs, loggerOn
return target, nil return target, nil
} }
// DrainBody close non nil response with any response Body.
// convenient wrapper to drain any remaining data on response body.
//
// Subsequently this allows golang http RoundTripper
// to re-use the same connection for future requests.
func DrainBody(respBody io.ReadCloser) {
// Callers should close resp.Body when done reading from it.
// If resp.Body is not closed, the Client's underlying RoundTripper
// (typically Transport) may not be able to re-use a persistent TCP
// connection to the server for a subsequent "keep-alive" request.
if respBody != nil {
// Drain any remaining Body and then close the connection.
// Without this closing connection would disallow re-using
// the same connection for future uses.
// - http://stackoverflow.com/a/17961593/4465767
defer respBody.Close()
io.CopyN(io.Discard, respBody, 1*humanize.MiByte)
}
}