golangci: enable bodyclose checker and fix related code
It has found an issue in the oracle code, so I think it's worth doing.
This commit is contained in:
parent
df9a4fa7ce
commit
3c009271f8
7 changed files with 16 additions and 7 deletions
|
@ -42,6 +42,7 @@ linters:
|
||||||
|
|
||||||
# extra linters
|
# extra linters
|
||||||
# - exhaustive
|
# - exhaustive
|
||||||
|
- bodyclose
|
||||||
- gofmt
|
- gofmt
|
||||||
- whitespace
|
- whitespace
|
||||||
- goimports
|
- goimports
|
||||||
|
|
|
@ -90,7 +90,10 @@ var errConnClosedByUser = errors.New("connection closed by user")
|
||||||
// operating on.
|
// operating on.
|
||||||
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
|
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
|
||||||
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
||||||
ws, _, err := dialer.Dial(endpoint, nil)
|
ws, resp, err := dialer.Dial(endpoint, nil)
|
||||||
|
if resp != nil && resp.Body != nil { // Can be non-nil even with error returned.
|
||||||
|
defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker.
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ func TestDefaultClient_RestrictedRedirectErr(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, c := range testCases {
|
for _, c := range testCases {
|
||||||
t.Run(c, func(t *testing.T) {
|
t.Run(c, func(t *testing.T) {
|
||||||
_, err := cl.Get(c)
|
_, err := cl.Get(c) //nolint:bodyclose // It errors out and it's a test.
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.True(t, errors.Is(err, ErrRestrictedRedirect), err)
|
require.True(t, errors.Is(err, ErrRestrictedRedirect), err)
|
||||||
require.True(t, strings.Contains(err.Error(), "IP is not global unicast"), err)
|
require.True(t, strings.Contains(err.Error(), "IP is not global unicast"), err)
|
||||||
|
|
|
@ -138,6 +138,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
||||||
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err), zap.Stringer("code", resp.Code))
|
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err), zap.Stringer("code", resp.Code))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
switch r.StatusCode {
|
switch r.StatusCode {
|
||||||
case http.StatusOK:
|
case http.StatusOK:
|
||||||
if !checkMediaType(r.Header.Get("Content-Type"), o.MainCfg.AllowedContentTypes) {
|
if !checkMediaType(r.Header.Get("Content-Type"), o.MainCfg.AllowedContentTypes) {
|
||||||
|
|
|
@ -67,8 +67,6 @@ func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) {
|
||||||
var ErrResponseTooLarge = errors.New("too big response")
|
var ErrResponseTooLarge = errors.New("too big response")
|
||||||
|
|
||||||
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
|
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
|
||||||
defer rc.Close()
|
|
||||||
|
|
||||||
buf := make([]byte, limit+1)
|
buf := make([]byte, limit+1)
|
||||||
n, err := gio.ReadFull(rc, buf)
|
n, err := gio.ReadFull(rc, buf)
|
||||||
if err == gio.ErrUnexpectedEOF && n <= limit {
|
if err == gio.ErrUnexpectedEOF && n <= limit {
|
||||||
|
|
|
@ -2633,8 +2633,9 @@ func checkErrGetBatchResult(t *testing.T, body []byte, expectingFail bool) json.
|
||||||
func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte {
|
func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte {
|
||||||
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
|
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
|
||||||
url = "ws" + strings.TrimPrefix(url, "http")
|
url = "ws" + strings.TrimPrefix(url, "http")
|
||||||
c, _, err := dialer.Dial(url+"/ws", nil)
|
c, r, err := dialer.Dial(url+"/ws", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer r.Body.Close()
|
||||||
err = c.SetWriteDeadline(time.Now().Add(time.Second))
|
err = c.SetWriteDeadline(time.Now().Add(time.Second))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.WriteMessage(1, []byte(rpcCall)))
|
require.NoError(t, c.WriteMessage(1, []byte(rpcCall)))
|
||||||
|
@ -2651,6 +2652,7 @@ func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte {
|
||||||
resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall))
|
resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall))
|
||||||
require.NoErrorf(t, err, "could not make a POST request")
|
require.NoErrorf(t, err, "could not make a POST request")
|
||||||
body, err := gio.ReadAll(resp.Body)
|
body, err := gio.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
|
assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
|
||||||
return bytes.TrimSpace(body)
|
return bytes.TrimSpace(body)
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,8 +59,9 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso
|
||||||
|
|
||||||
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
|
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
|
||||||
url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
|
url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
|
||||||
ws, _, err := dialer.Dial(url, nil)
|
ws, r, err := dialer.Dial(url, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Use buffered channel to read server's messages and then read expected
|
// Use buffered channel to read server's messages and then read expected
|
||||||
// responses from it.
|
// responses from it.
|
||||||
|
@ -520,7 +521,10 @@ func TestWSClientsLimit(t *testing.T) {
|
||||||
wss := make([]*websocket.Conn, maxSubscribers)
|
wss := make([]*websocket.Conn, maxSubscribers)
|
||||||
|
|
||||||
for i := 0; i < len(wss)+1; i++ {
|
for i := 0; i < len(wss)+1; i++ {
|
||||||
ws, _, err := dialer.Dial(url, nil)
|
ws, r, err := dialer.Dial(url, nil)
|
||||||
|
if r != nil && r.Body != nil {
|
||||||
|
defer r.Body.Close()
|
||||||
|
}
|
||||||
if i < maxSubscribers {
|
if i < maxSubscribers {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
wss[i] = ws
|
wss[i] = ws
|
||||||
|
|
Loading…
Reference in a new issue