Skip to content

Commit 96ef9c3

Browse files
committed
Fix order of body read and close
1 parent 4ce85ca commit 96ef9c3

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

internal/benchrunner/runners/rally/runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,11 @@ func (r *runner) bulkMetrics(indexName string, sr searchResponse) error {
811811
if err != nil {
812812
return fmt.Errorf("error executing scroll: %s", err)
813813
}
814+
defer resp.Body.Close()
815+
814816
if resp.IsError() {
815817
return fmt.Errorf("error executing scroll: %s", resp.String())
816818
}
817-
resp.Body.Close()
818819

819820
return nil
820821
}

internal/dump/ilmpolicies.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ func getILMPolicyByName(ctx context.Context, api *elasticsearch.API, policy stri
5757
}
5858
defer resp.Body.Close()
5959

60+
if resp.IsError() {
61+
return nil, fmt.Errorf("failed to get policy %s: %s", policy, resp.String())
62+
}
63+
6064
d, err := io.ReadAll(resp.Body)
6165
if err != nil {
6266
return nil, fmt.Errorf("failed to read response body: %w", err)
6367
}
6468

65-
if resp.IsError() {
66-
return nil, fmt.Errorf("failed to get policy %s: %s", policy, resp.String())
67-
}
68-
6969
var policiesResponse getILMLifecycleResponse
7070
err = json.Unmarshal(d, &policiesResponse)
7171
if err != nil {

internal/dump/indextemplates.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ func getIndexTemplatesForPackage(ctx context.Context, api *elasticsearch.API, pa
7777
}
7878
defer resp.Body.Close()
7979

80+
if resp.IsError() {
81+
return nil, fmt.Errorf("failed to get index templates: %s", resp.String())
82+
}
83+
8084
d, err := io.ReadAll(resp.Body)
8185
if err != nil {
8286
return nil, fmt.Errorf("failed to read response body: %w", err)
8387
}
8488

85-
if resp.IsError() {
86-
return nil, fmt.Errorf("failed to get index templates: %s", resp.String())
87-
}
88-
8989
var templateResponse getIndexTemplateResponse
9090
err = json.Unmarshal(d, &templateResponse)
9191
if err != nil {

internal/elasticsearch/client.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ func (client *Client) CheckHealth(ctx context.Context) error {
132132
}
133133
defer resp.Body.Close()
134134

135+
if resp.StatusCode != http.StatusOK {
136+
return fmt.Errorf("failed to check cluster health: %s", resp.String())
137+
}
138+
135139
body, err := io.ReadAll(resp.Body)
136140
if err != nil {
137141
return fmt.Errorf("error reading cluster health response: %w", err)
138142
}
139143

140-
if resp.StatusCode != http.StatusOK {
141-
return fmt.Errorf("failed to check cluster health; API status code = %d; response body = %s", resp.StatusCode, string(body))
142-
}
143-
144144
var clusterHealth struct {
145145
Status string `json:"status"`
146146
}

0 commit comments

Comments
 (0)