From ca26551982fd31d8ab899c56e837c71d577372f5 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Wed, 6 Mar 2024 11:01:02 -0800 Subject: [PATCH] fix errors reported by staticcheck --- .github/workflows/ci.yml | 4 +++- cmd/nsq-to-http/main.go | 7 +++++-- consumer.go | 7 +++---- error.go | 7 ------- lookup.go | 5 ++--- nsq.go | 5 ++--- nsqlookup/consul.go | 5 ++--- nsqlookup/engine.go | 4 ---- nsqlookup/proxy.go | 9 +++------ nsqlookup/resolver.go | 2 +- response.go | 1 + 11 files changed, 22 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f7a04a..2e297a8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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' diff --git a/cmd/nsq-to-http/main.go b/cmd/nsq-to-http/main.go index 3617de1..fd92d1d 100644 --- a/cmd/nsq-to-http/main.go +++ b/cmd/nsq-to-http/main.go @@ -2,7 +2,7 @@ package main import ( "bytes" - "io/ioutil" + "io" "log" "math" "math/rand" @@ -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) @@ -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))) diff --git a/consumer.go b/consumer.go index c592a15..566d9a1 100644 --- a/consumer.go +++ b/consumer.go @@ -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) @@ -354,7 +354,6 @@ func (c *Consumer) close() { sendCommand(cm.CmdChan, Cls{}) } c.mtx.Unlock() - return } func (c *Consumer) closeConn(addr string) { diff --git a/error.go b/error.go index f617c2d..b35fcc7 100644 --- a/error.go +++ b/error.go @@ -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() -} diff --git a/lookup.go b/lookup.go index 69c0046..6fe3281 100644 --- a/lookup.go +++ b/lookup.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "net/http" "net/url" "time" @@ -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{ @@ -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 } diff --git a/nsq.go b/nsq.go index 77f62ec..75adb06 100644 --- a/nsq.go +++ b/nsq.go @@ -3,7 +3,6 @@ package nsq import ( "bytes" "io" - "io/ioutil" "net/http" "net/url" @@ -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{ @@ -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 } diff --git a/nsqlookup/consul.go b/nsqlookup/consul.go index 781206b..615cd3a 100644 --- a/nsqlookup/consul.go +++ b/nsqlookup/consul.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "path" "sort" @@ -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, @@ -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 diff --git a/nsqlookup/engine.go b/nsqlookup/engine.go index ca25555..923d215 100644 --- a/nsqlookup/engine.go +++ b/nsqlookup/engine.go @@ -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 { diff --git a/nsqlookup/proxy.go b/nsqlookup/proxy.go index 26ad0aa..6bbf01e 100644 --- a/nsqlookup/proxy.go +++ b/nsqlookup/proxy.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -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 } @@ -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: @@ -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 } @@ -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 { diff --git a/nsqlookup/resolver.go b/nsqlookup/resolver.go index f578889..5d04f3e 100644 --- a/nsqlookup/resolver.go +++ b/nsqlookup/resolver.go @@ -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 } diff --git a/response.go b/response.go index fc5ae5a..aab60d2 100644 --- a/response.go +++ b/response.go @@ -37,6 +37,7 @@ func (r Response) FrameType() FrameType { 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") + return } if _, err = w.WriteString(string(r)); err != nil {