Skip to content

Commit

Permalink
all: fix errors reported by staticcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinburkesegment committed Mar 6, 2024
1 parent 7bf22ca commit 79edaca
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 47 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
# github.com/actions/setup-go/tags
uses: actions/setup-go@v5
with:
go-version: 1.22.x
# github.com/actions/checkout/tags
- uses: actions/checkout@v4
with:
path: './src/github.com/segmentio/nsq-go'
Expand Down
7 changes: 5 additions & 2 deletions cmd/nsq-to-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"bytes"
"io/ioutil"
"io"
"log"
"math"
"math/rand"
Expand Down Expand Up @@ -76,6 +76,9 @@ func main() {
MaxInFlight: config.MaxInFlight,
Identify: nsq.Identify{UserAgent: config.UserAgent},
})
if err != nil {
log.Fatalf("could not start consumer: %v", err)
}

wg := sync.WaitGroup{}
wg.Add(config.MaxInFlight)
Expand Down Expand Up @@ -118,7 +121,7 @@ func forward(dst *url.URL, contentType, userAgent string, timeout time.Duration,
"Content-Type": {contentType},
"User-Agent": {userAgent},
},
Body: ioutil.NopCloser(bytes.NewReader(msg.Body)),
Body: io.NopCloser(bytes.NewReader(msg.Body)),
ContentLength: int64(len(msg.Body)),
}).WithContext(timers.LowRes.Timeout(timeout)))

Expand Down
7 changes: 3 additions & 4 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ func (c *Consumer) run() {
go func(cm connMeta) {
start := time.Now()
for len(cm.CmdChan) > 0 {
if time.Now().Sub(start) > c.drainTimeout {
log.Print("failed to drain CmdChan for connection, closing now")
if time.Since(start) > c.drainTimeout {
log.Println("failed to drain CmdChan for connection, closing now")
break
}
log.Println("awaiting for write channel to flush any requeue commands")
log.Println("waiting for write channel to flush any requeue commands")
time.Sleep(time.Millisecond * 500)
}
closeCommand(cm.CmdChan)
Expand Down Expand Up @@ -354,7 +354,6 @@ func (c *Consumer) close() {
sendCommand(cm.CmdChan, Cls{})
}
c.mtx.Unlock()
return
}

func (c *Consumer) closeConn(addr string) {
Expand Down
7 changes: 0 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,3 @@ func appendError(errList error, err error) error {
return errorList{err}
}
}

func isTimeout(err error) bool {
e, ok := err.(interface {
Timeout() bool
})
return ok && e.Timeout()
}
5 changes: 2 additions & 3 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -156,7 +155,7 @@ func (c *LookupClient) do(host string, method string, path string, query url.Val
}

if len(data) != 0 {
body = ioutil.NopCloser(bytes.NewReader(data))
body = io.NopCloser(bytes.NewReader(data))
}

if res, err = c.Do(&http.Request{
Expand Down Expand Up @@ -184,7 +183,7 @@ func (c *LookupClient) do(host string, method string, path string, query url.Val

defer res.Body.Close()

if ret, err = ioutil.ReadAll(res.Body); err != nil {
if ret, err = io.ReadAll(res.Body); err != nil {
err = errors.Wrap(err, "reading response body")
return
}
Expand Down
5 changes: 2 additions & 3 deletions nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nsq
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -125,7 +124,7 @@ func (c *Client) do(method string, path string, query url.Values, data []byte) (
}

if len(data) != 0 {
body = ioutil.NopCloser(bytes.NewReader(data))
body = io.NopCloser(bytes.NewReader(data))
}

if res, err = c.Do(&http.Request{
Expand Down Expand Up @@ -157,7 +156,7 @@ func (c *Client) do(method string, path string, query url.Values, data []byte) (
return
}

if ret, err = ioutil.ReadAll(res.Body); err != nil {
if ret, err = io.ReadAll(res.Body); err != nil {
err = errors.Wrapf(err, "%s %s://%s?%s", method, scheme, host, query.Encode())
return
}
Expand Down
5 changes: 2 additions & 3 deletions nsqlookup/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"path"
"sort"
Expand Down Expand Up @@ -428,7 +427,7 @@ func (e *ConsulEngine) do(ctx context.Context, method string, url string, send i
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
io.Copy(ioutil.Discard, res.Body)
io.Copy(io.Discard, res.Body)
err = consulError{
method: method,
url: url,
Expand All @@ -443,7 +442,7 @@ func (e *ConsulEngine) do(ctx context.Context, method string, url string, send i
return
}
} else {
io.Copy(ioutil.Discard, res.Body)
io.Copy(io.Discard, res.Body)
}

return
Expand Down
4 changes: 0 additions & 4 deletions nsqlookup/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ func httpBroadcastAddress(info NodeInfo) string {
return makeBroadcastAddress(info.BroadcastAddress, info.HttpPort)
}

func tcpBroadcastAddress(info NodeInfo) string {
return makeBroadcastAddress(info.BroadcastAddress, info.TcpPort)
}

func makeBroadcastAddress(addr string, port int) string {
host, _, _ := net.SplitHostPort(addr)
if len(host) == 0 {
Expand Down
9 changes: 3 additions & 6 deletions nsqlookup/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -382,7 +381,7 @@ func (e *ProxyEngine) do(ctx context.Context, method string, url string, send in
r = bytes.NewReader(b)
}

if strings.Index(url, "://") < 0 {
if !strings.Contains(url, "://") {
url = "http://" + url
}

Expand All @@ -405,7 +404,7 @@ func (e *ProxyEngine) do(ctx context.Context, method string, url string, send in
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
io.Copy(ioutil.Discard, res.Body)
io.Copy(io.Discard, res.Body)
err = errNotFound
return
default:
Expand All @@ -422,7 +421,7 @@ func (e *ProxyEngine) do(ctx context.Context, method string, url string, send in
}

if recv == nil {
io.Copy(ioutil.Discard, res.Body)
io.Copy(io.Discard, res.Body)
return
}

Expand All @@ -431,8 +430,6 @@ func (e *ProxyEngine) do(ctx context.Context, method string, url string, send in
}

type ProxyNode struct {
server string
info NodeInfo
}

type proxyError struct {
Expand Down
2 changes: 1 addition & 1 deletion nsqlookup/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *ConsulRegistry) get(ctx context.Context, endpoint string, result interf
address = "http://localhost:8500"
}

if strings.Index(address, "://") < 0 {
if !strings.Contains(address, "://") {
address = "http://" + address
}

Expand Down
23 changes: 10 additions & 13 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,24 @@ func (r Response) FrameType() FrameType {

// Write serializes the frame to the given buffered output, satisfies the Frame
// interface.
func (r Response) Write(w *bufio.Writer) (err error) {
if err = writeFrameHeader(w, FrameTypeResponse, len(r)); err != nil {
err = errors.WithMessage(err, "writing response message")
func (r Response) Write(w *bufio.Writer) error {
if err := writeFrameHeader(w, FrameTypeResponse, len(r)); err != nil {
return errors.WithMessage(err, "writing response message")
}

if _, err = w.WriteString(string(r)); err != nil {
err = errors.Wrap(err, "writing response message")
return
if _, err := w.WriteString(string(r)); err != nil {
return errors.Wrap(err, "writing response message")
}

return
return nil
}

func readResponse(n int, r *bufio.Reader) (res Response, err error) {
func readResponse(n int, r *bufio.Reader) (Response, error) {
data := make([]byte, n)

if _, err = io.ReadFull(r, data); err != nil {
err = errors.Wrap(err, "reading response message")
return
if _, err := io.ReadFull(r, data); err != nil {
return "", errors.Wrap(err, "reading response message")
}

res = Response(data)
return
return Response(data), nil
}

0 comments on commit 79edaca

Please sign in to comment.