Skip to content

Commit f2df643

Browse files
committed
all: add bodyclose linter
1 parent 4832d90 commit f2df643

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

.golangci-lint.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
linters:
2+
enable:
3+
- bodyclose
4+
15
issues:
26
exclude-rules:
37
- path: '(.+)_test\.go'

pkg/code/webhook/execution.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313

1414
messagingpb "github.com/code-payments/code-protobuf-api/generated/go/messaging/v1"
1515

16-
"github.com/code-payments/code-server/pkg/metrics"
1716
"github.com/code-payments/code-server/pkg/code/common"
1817
code_data "github.com/code-payments/code-server/pkg/code/data"
1918
"github.com/code-payments/code-server/pkg/code/data/webhook"
2019
"github.com/code-payments/code-server/pkg/code/server/grpc/messaging"
20+
"github.com/code-payments/code-server/pkg/metrics"
2121
)
2222

2323
const (
@@ -95,7 +95,10 @@ func Execute(
9595
resp, err := http.DefaultClient.Do(webhookReq)
9696
if err != nil {
9797
return errors.Wrap(err, "error executing http post request")
98-
} else if resp.StatusCode != http.StatusOK {
98+
}
99+
defer resp.Body.Close()
100+
101+
if resp.StatusCode != http.StatusOK {
99102
return errors.Errorf("%d status code returned", resp.StatusCode)
100103
}
101104

pkg/currency/coingecko/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func (c *client) submitRequest(ctx context.Context, url string, body io.Reader,
8989
var httpResp *http.Response
9090
_, err = c.retrier.Retry(
9191
func() error {
92-
httpResp, err = c.httpClient.Do(req)
92+
// Retry only occurs if err != nil, in which case the body does not need to be closed.
93+
// The body itself is closed below
94+
httpResp, err = c.httpClient.Do(req) //nolint:bodyclose
9395
return err
9496
},
9597
)

pkg/currency/fixer/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func (c *client) submitRequest(ctx context.Context, url string, resp interface{}
113113
var httpResp *http.Response
114114
_, err = c.retrier.Retry(
115115
func() error {
116-
httpResp, err = c.httpClient.Do(req)
116+
// Retry only occurs if err != nil, in which case the body does not need to be closed.
117+
// The body itself is closed below
118+
httpResp, err = c.httpClient.Do(req) //nolint:bodyclose
117119
return err
118120
},
119121
)

pkg/netutil/url.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ func ValidateHttpUrl(
5151
var resp *http.Response
5252
_, err = retry.Retry(
5353
func() error {
54-
resp, err = http.Get(value)
54+
// Retry only occurs if err != nil, in which case the body does not need to be closed.
55+
// The body itself is closed below
56+
resp, err = http.Get(value) //nolint:bodyclose
5557
return err
5658
},
5759
retry.Limit(5),
@@ -60,6 +62,7 @@ func ValidateHttpUrl(
6062
if err != nil {
6163
return err
6264
}
65+
defer resp.Body.Close()
6366

6467
if resp.StatusCode != http.StatusOK {
6568
return errors.Errorf("%d status code fetching content", resp.StatusCode)

0 commit comments

Comments
 (0)