Skip to content

Commit 0876d29

Browse files
committed
chore(osgen): drop dead body-drain in plugin do() no-decode path
opensearch.Do routes through the buffered (*opensearchtransport.Client).Perform, so resp.Body in the plugin do[T] helpers is already an io.NopCloser over a bytes.Reader -- the connection has been drained and returned to the pool. The no-decode error-path drain added in PR opensearch-project#859 was only meaningful when DisableResponseBuffering=true, which has been removed. Strip the drain from the cmd/osgen plugin client template and the four hand-written copies (opensearchapi, v5preview/opensearchapi, plugins/security, plugins/ism). The helper now reads: if resp.IsError() { if dataPointer != nil { return resp, opensearch.ParseError(resp) } return resp, fmt.Errorf("status: %s", resp.Status()) } with a doc comment noting resp.Body has already been buffered and closed by Perform. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 11d2ea1 commit 0876d29

5 files changed

Lines changed: 25 additions & 14 deletions

File tree

cmd/osgen/emit/frag_plugin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func NewClient(client *opensearch.Client) *Client {
146146
}
147147
148148
// do calls [opensearch.Do] and checks the response for errors.
149+
//
150+
// [opensearch.Do] routes through the buffered [opensearchtransport.Client.Perform],
151+
// so resp.Body here is already an [io.NopCloser] over a [bytes.Reader] -- the
152+
// connection has been drained and returned to the pool. The helper only needs
153+
// to translate IsError into a typed error.
149154
func do[T any](ctx context.Context, c *Client, method string, req opensearch.Request, dataPointer *T) (*opensearch.Response, error) {
150155
resp, err := opensearch.Do(ctx, c.Client, method, req, dataPointer)
151156
if err != nil {

opensearchapi/opensearchapi.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ func NewFromClientWithErrors(client *opensearch.Client) *Client {
174174

175175
// do calls [opensearch.Do] and checks the response for OpenSearch API errors.
176176
// The generic *T parameter enforces that dataPointer is a pointer at compile time.
177+
//
178+
// [opensearch.Do] routes through the buffered [opensearchtransport.Client.Perform],
179+
// so resp.Body here is already an [io.NopCloser] over a [bytes.Reader] -- the
180+
// connection has been drained and returned to the pool. The helper only needs
181+
// to translate IsError into a typed error.
177182
func do[T any](ctx context.Context, c *Client, method string, req opensearch.Request, dataPointer *T) (*opensearch.Response, error) {
178183
resp, err := opensearch.Do(ctx, c.Client, method, req, dataPointer)
179184
if err != nil {

plugins/ism/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func NewClient(config Config) (*Client, error) {
4545

4646
// do calls [opensearch.Do] and checks the response for errors.
4747
// The generic *T parameter enforces that dataPointer is a pointer at compile time.
48+
//
49+
// [opensearch.Do] routes through the buffered [opensearchtransport.Client.Perform],
50+
// so resp.Body here is already an [io.NopCloser] over a [bytes.Reader] -- the
51+
// connection has been drained and returned to the pool. The helper only needs
52+
// to translate IsError into a typed error.
4853
func do[T any](ctx context.Context, c *Client, method string, req opensearch.Request, dataPointer *T) (*opensearch.Response, error) {
4954
resp, err := opensearch.Do(ctx, c.Client, method, req, dataPointer)
5055
if err != nil {

plugins/security/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func NewClient(config Config) (*Client, error) {
6363

6464
// do calls [opensearch.Do] and checks the response for errors.
6565
// The generic *T parameter enforces that dataPointer is a pointer at compile time.
66+
//
67+
// [opensearch.Do] routes through the buffered [opensearchtransport.Client.Perform],
68+
// so resp.Body here is already an [io.NopCloser] over a [bytes.Reader] -- the
69+
// connection has been drained and returned to the pool. The helper only needs
70+
// to translate IsError into a typed error.
6671
func do[T any](ctx context.Context, c *Client, method string, req opensearch.Request, dataPointer *T) (*opensearch.Response, error) {
6772
resp, err := opensearch.Do(ctx, c.Client, method, req, dataPointer)
6873
if err != nil {

v5preview/opensearchapi/api.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
package opensearchapi
88

99
import (
10-
"bytes"
1110
"context"
1211
"fmt"
13-
"io"
1412
"net"
1513
"os"
1614
"strconv"
@@ -236,6 +234,11 @@ func (c *Client) Clone() *Client {
236234
}
237235

238236
// do calls [opensearch.Do] and checks the response for OpenSearch API errors.
237+
//
238+
// [opensearch.Do] routes through the buffered [opensearchtransport.Client.Perform],
239+
// so resp.Body here is already an [io.NopCloser] over a [bytes.Reader] -- the
240+
// connection has been drained and returned to the pool. The helper only needs
241+
// to translate IsError into a typed error.
239242
func do[T any](ctx context.Context, c *Client, method string, req opensearch.Request, dataPointer *T) (*opensearch.Response, error) {
240243
resp, err := opensearch.Do(ctx, c.Client, method, req, dataPointer)
241244
if err != nil {
@@ -246,18 +249,6 @@ func do[T any](ctx context.Context, c *Client, method string, req opensearch.Req
246249
if dataPointer != nil {
247250
return resp, opensearch.ParseError(resp)
248251
}
249-
// Read the error body to completion and re-wrap it. Reading to EOF
250-
// lets http.Transport reuse the connection when DisableResponseBuffering
251-
// is set; re-wrapping keeps resp.Body readable for the caller, matching
252-
// the dataPointer != nil branch (which routes through ParseError). In the
253-
// default buffered mode the body is already an in-memory NopCloser, so the
254-
// read is cheap and non-destructive.
255-
if resp.Body != nil {
256-
//nolint:errcheck // best-effort drain so the connection can be reused
257-
body, _ := io.ReadAll(resp.Body)
258-
resp.Body.Close()
259-
resp.Body = io.NopCloser(bytes.NewReader(body))
260-
}
261252
return resp, fmt.Errorf("status: %s", resp.Status())
262253
}
263254

0 commit comments

Comments
 (0)