Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: defer close body (#12461)

p0nybe48be424180
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.go
index 169189eab5..f2111b24e0 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch.go
@@ -171,51 +171,56 @@ func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ *
var results []*v1pb.QueryResult
for _, s := range statements {
- startTime := time.Now()
- // send HTTP request.
- resp, err := d.basicAuthClient.Do(s.method, s.route, s.queryString)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to send HTTP request, status code message: %s", resp.Status)
- }
+ if err := func() error {
+ startTime := time.Now()
+ // send HTTP request.
+ resp, err := d.basicAuthClient.Do(s.method, s.route, s.queryString)
+ if err != nil {
+ return errors.Wrapf(err, "failed to send HTTP request, status code message: %s", resp.Status)
+ }
+ defer resp.Body.Close()
- respBytes, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- if err := resp.Body.Close(); err != nil {
- return nil, err
- }
+ respBytes, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+ if err := resp.Body.Close(); err != nil {
+ return err
+ }
- // structure results.
- var result v1pb.QueryResult
- var row v1pb.QueryRow
+ // structure results.
+ var result v1pb.QueryResult
+ var row v1pb.QueryRow
- contentType := resp.Header.Get("Content-Type")
- if strings.Contains(contentType, "application/json") {
- pairs := map[string]any{}
- if err := json.Unmarshal(respBytes, &pairs); err != nil {
- return nil, errors.Wrapf(err, "failed to parse json body")
- }
- for key, val := range pairs {
- result.ColumnNames = append(result.ColumnNames, key)
- bytes, err := json.Marshal(val)
- if err != nil {
- return nil, err
+ contentType := resp.Header.Get("Content-Type")
+ if strings.Contains(contentType, "application/json") {
+ pairs := map[string]any{}
+ if err := json.Unmarshal(respBytes, &pairs); err != nil {
+ return errors.Wrapf(err, "failed to parse json body")
}
- row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: string(bytes)}})
+ for key, val := range pairs {
+ result.ColumnNames = append(result.ColumnNames, key)
+ bytes, err := json.Marshal(val)
+ if err != nil {
+ return err
+ }
+ row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: string(bytes)}})
+ }
+ } else if strings.Contains(contentType, "text/plain") {
+ result.ColumnNames = append(result.ColumnNames, "text/plain")
+ row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: string(respBytes)}})
+ } else {
+ return errors.Errorf("Content-Type not supported: %s", contentType)
}
- } else if strings.Contains(contentType, "text/plain") {
- result.ColumnNames = append(result.ColumnNames, "text/plain")
- row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: string(respBytes)}})
- } else {
- return nil, errors.Errorf("Content-Type not supported: %s", contentType)
+ result.Rows = append(result.Rows, &row)
+ result.Latency = durationpb.New(time.Since(startTime))
+ result.Statement = fmt.Sprintf("%s %s\n%s", s.method, s.route, s.queryString)
+ // TODO(d): handle max size.
+ results = append(results, &result)
+ return nil
+ }(); err != nil {
+ return nil, err
}
-
- result.Rows = append(result.Rows, &row)
- result.Latency = durationpb.New(time.Since(startTime))
- result.Statement = fmt.Sprintf("%s %s\n%s", s.method, s.route, s.queryString)
- // TODO(d): handle max size.
- results = append(results, &result)
}
return results, nil
diff --git a/backend/plugin/db/elasticsearch/sync.go b/backend/plugin/db/elasticsearch/sync.go
index b421a02216..98d13fff4a 100644
--- a/backend/plugin/db/elasticsearch/sync.go
+++ b/backend/plugin/db/elasticsearch/sync.go
@@ -94,6 +94,7 @@ func (d *Driver) getVerison() (string, error) {
if err != nil {
return "", err
}
+ defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index 6f1ff6c372..1dc8ab46fd 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -166,6 +166,7 @@ func registerRDSMysqlCerts(ctx context.Context) error {
if err != nil {
return err
}
+ defer resp.Body.Close()
pem, err := io.ReadAll(resp.Body)
if err != nil {
diff --git a/backend/plugin/vcs/internal/request.go b/backend/plugin/vcs/internal/request.go
index dc8abead46..d01b8d3fb2 100644
--- a/backend/plugin/vcs/internal/request.go
+++ b/backend/plugin/vcs/internal/request.go
@@ -45,6 +45,8 @@ func request(ctx context.Context, method, url string, authorization string, head
if err != nil {
return 0, "", errors.Wrapf(err, "failed to send delete request for url %s", url)
}
+ defer resp.Body.Close()
+
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return 0, "", errors.Wrapf(err, "read delete %s response body with status code %d", url, resp.StatusCode)