Skip to content

Commit

Permalink
Preallocate buffer if possible
Browse files Browse the repository at this point in the history
Signed-off-by: James Pond <[email protected]>
  • Loading branch information
jamesponddotco committed Jan 15, 2024
1 parent 5c6c52c commit 38cfab5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,15 @@ func (c *Client) do(ctx context.Context, req *http.Request) (*Response, error) {
return nil, fmt.Errorf("%w: %d", ErrRequestFailed, resp.StatusCode)
}

var buffer bytes.Buffer
var buffer *bytes.Buffer

_, err = io.Copy(&buffer, resp.Body)
if resp.ContentLength > 0 {
buffer = bytes.NewBuffer(make([]byte, 0, resp.ContentLength))
} else {
buffer = bytes.NewBuffer(make([]byte, 0))
}

_, err = io.Copy(buffer, resp.Body)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
Expand Down

0 comments on commit 38cfab5

Please sign in to comment.