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 23, 2023
1 parent 486e5e9 commit f889dc7
Show file tree
Hide file tree
Showing 3 changed files with 37 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
28 changes: 28 additions & 0 deletions opensearchtransport/opensearchtransport_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// specific language governing permissions and limitations
// under the License.

//go:build integration
// +build integration

package opensearchtransport_test
Expand Down Expand Up @@ -121,6 +122,33 @@ func TestTransportHeaders(t *testing.T) {
}
}

func TestTransportBodyClose(t *testing.T) {
u, _ := url.Parse("http://localhost:9200")

tp, _ := opensearchtransport.New(opensearchtransport.Config{
URLs: []*url.URL{u},
})

req, _ := http.NewRequest("GET", "/", nil)
res, err := tp.Perform(req)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if closeResp := res.Body.Close(); closeResp != nil {
t.Fatalf("Unexpected return on res.Body.Close(): %s", closeResp)
}
if closeResp := res.Body.Close(); closeResp != nil {
t.Fatalf("Unexpected return on res.Body.Close(): %s", closeResp)
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("Failed to read the reponse body: %s", err)
}
if body == nil || len(body) == 0 {
t.Fatalf("Unexpected response body:\n%s", body)
}
}

func TestTransportCompression(t *testing.T) {
var req *http.Request
var res *http.Response
Expand Down

0 comments on commit f889dc7

Please sign in to comment.