Skip to content

Commit

Permalink
chore(repo): close response bodies to prevent resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
xqqp committed Oct 8, 2024
1 parent 6845dab commit 1cca1f6
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ linters:
- unconvert
- unused
- intrange
- bodyclose
9 changes: 8 additions & 1 deletion contrib/jepsen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"context"
"errors"
"fmt"
"github.com/golang/glog"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -216,7 +217,13 @@ func jepsenServe() error {
// Check if the page is already up
checkServing := func() error {
url := jepsenURL()
_, err := http.Get(url) // nolint:gosec
resp, err := http.Get(url) // nolint:gosec
defer func() {
err = resp.Body.Close()
if err != nil {
glog.Errorf("Error while closing response body: %s", err)
}
}()
return err
}
if err := checkServing(); err == nil {
Expand Down
8 changes: 7 additions & 1 deletion graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ type GqlSchema struct {
}

func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, error) {

request, err := http.NewRequest("GET", "http://"+authority+"/probe/graphql", nil)
if err != nil {
return nil, err
Expand All @@ -263,6 +262,12 @@ func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, erro
if err != nil {
return nil, err
}
defer func() {
err = resp.Body.Close()
if err != nil {
glog.Errorf("Error while closing response body: %s", err)
}
}()

probeResp := ProbeGraphQLResp{}
if resp.StatusCode == http.StatusOK {
Expand Down Expand Up @@ -592,6 +597,7 @@ func safelyDropAll(t *testing.T, withGroot bool) {
func updateGQLSchemaUsingAdminSchemaEndpt(t *testing.T, authority, schema string) string {
resp, err := http.Post("http://"+authority+"/admin/schema", "", strings.NewReader(schema))
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func touchedUidsHeader(t *testing.T) {
client := http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

// confirm that the header value is a non-negative integer
touchedUidsInHeader, err := strconv.ParseUint(resp.Header.Get("Graphql-TouchedUids"), 10, 64)
Expand Down Expand Up @@ -116,6 +117,7 @@ func cacheControlHeader(t *testing.T) {
client := http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

// confirm that the header value is a non-negative integer
require.Equal(t, "public,max-age=5", resp.Header.Get("Cache-Control"))
Expand Down
10 changes: 8 additions & 2 deletions graphql/e2e/common/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/golang/glog"
"math/rand"
"net/http"

Expand Down Expand Up @@ -68,11 +69,16 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload

dialer := websocket.DefaultDialer
dialer.EnableCompression = true
conn, _, err := dialer.Dial(url, header)
conn, resp, err := dialer.Dial(url, header)
if err != nil {
return nil, err
}

defer func() {
err = resp.Body.Close()
if err != nil {
glog.Errorf("Error while closing response body: %s", err)
}
}()
// Initialize subscription.
init := operationMessage{
Type: initMsg,
Expand Down
8 changes: 8 additions & 0 deletions graphql/resolve/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ func sendWebhookEvent(ctx context.Context, m schema.Mutation, commitTs uint64, r
if err != nil {
glog.V(3).Info(errors.Wrap(err, "unable to send webhook event"))
}

defer func() {
err = resp.Body.Close()
if err != nil {
glog.Errorf("Error while closing response body: %s", err)
}
}()

if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
glog.V(3).Info(errors.Errorf("got unsuccessful status from webhook: %s", resp.Status))
}
Expand Down
6 changes: 4 additions & 2 deletions systest/backup/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func AddItem(t *testing.T, minSuffixVal int, maxSuffixVal int, jwtToken string,
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)

var data interface{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
require.NoError(t, resp.Body.Close())
}
}

Expand Down Expand Up @@ -189,8 +189,8 @@ func CheckItemExists(t *testing.T, desriedSuffix int, jwtToken string, whichAlph
}
client := &http.Client{}
resp, err := client.Do(req)

require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

var data interface{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
Expand Down Expand Up @@ -229,6 +229,7 @@ func TakeBackup(t *testing.T, jwtToken string, backupDst string, whichAlpha stri
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

var data interface{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
Expand Down Expand Up @@ -265,6 +266,7 @@ func RunRestore(t *testing.T, jwtToken string, restoreLocation string, whichAlph
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()

var data interface{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
Expand Down
6 changes: 6 additions & 0 deletions systest/bulk_live/common/bulk_live_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,12 @@ func matchExportCount(opts matchExport) error {
if err != nil {
return err
}
defer func() {
err = resp.Body.Close()
if err != nil {
glog.Errorf("Error while closing response body: %s", err)
}
}()

b, err = io.ReadAll(resp.Body)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions testutil/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func WaitForRestore(t *testing.T, dg *dgo.Dgraph, HttpSocket string) {
resp, err := http.Get("http://" + HttpSocket + "/health")
require.NoError(t, err)
buf, err := io.ReadAll(resp.Body)
require.NoError(t, resp.Body.Close())
require.NoError(t, err)
sbuf := string(buf)
if !strings.Contains(sbuf, "opRestore") {
Expand Down

0 comments on commit 1cca1f6

Please sign in to comment.