Skip to content

Commit

Permalink
Read, close and replace the http Reponse Body
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD committed Apr 14, 2023
1 parent 486e5e9 commit eb5be7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Removes the need for double error checking ([#246](https://github.com/opensearch-project/opensearch-go/pull/246))
- Updates workflows to reduce CI time, consolidate OpenSearch versions, update compatibility matrix ([#242](https://github.com/opensearch-project/opensearch-go/pull/242))
- Moved @svencowart to emeritus maintainers ([#270](https://github.com/opensearch-project/opensearch-go/pull/270))
- Read, close and replace the http Reponse Body ([#300](https://github.com/opensearch-project/opensearch-go/pull/300))

### Deprecated

Expand Down
15 changes: 8 additions & 7 deletions opensearchtransport/opensearchtransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ func init() {
}

// Interface defines the interface for HTTP client.
//
type Interface interface {
Perform(*http.Request) (*http.Response, error)
}

// Config represents the configuration of HTTP client.
//
type Config struct {
URLs []*url.URL
Username string
Expand Down Expand Up @@ -113,7 +111,6 @@ type Config struct {
}

// Client represents the HTTP client.
//
type Client struct {
sync.Mutex

Expand Down Expand Up @@ -146,7 +143,6 @@ type Client struct {
// New creates new transport client.
//
// http.DefaultTransport will be used if no transport is passed in the configuration.
//
func New(cfg Config) (*Client, error) {
if cfg.Transport == nil {
cfg.Transport = http.DefaultTransport
Expand Down Expand Up @@ -235,7 +231,6 @@ func New(cfg Config) (*Client, error) {
}

// Perform executes the request and returns a response or error.
//
func (c *Client) Perform(req *http.Request) (*http.Response, error) {
var (
res *http.Response
Expand Down Expand Up @@ -407,14 +402,20 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
time.Sleep(c.retryBackoff(i + 1))
}
}
// Read, close and replace the http reponse body to close the connection
if res != nil && res.Body != nil {
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err == nil {
res.Body = io.NopCloser(bytes.NewReader(body))
}
}

// TODO(karmi): Wrap error
return res, err
}

// URLs returns a list of transport URLs.
//
//
func (c *Client) URLs() []*url.URL {
return c.pool.URLs()
}
Expand Down

0 comments on commit eb5be7f

Please sign in to comment.