Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: staticcheck linting cleanup #1427

Merged
merged 20 commits into from
Dec 21, 2022
53 changes: 21 additions & 32 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,44 @@ jobs:
strategy:
fail-fast: false
matrix:
imgtag:
- "golang:1.14-buster"
- "golang:1.15-buster"
- "golang:1.16-bullseye"
- "golang:1.17-bullseye"
goarch:
- "amd64"
- "386"
go: ["1.17.x", "1.18.x", "1.19.x"]
arch: ["amd64", "386"]

container: "${{matrix.imgtag}}"
env:
GOPATH: "${{github.workspace}}/go"
GOARCH: "${{matrix.goarch}}"
SRCDIR: "go/src/github.com/nsqio/nsq"
GOARCH: "${{matrix.arch}}"

steps:
- uses: actions/checkout@v2

- uses: WillAbides/setup-go-faster@v1.7.0
with:
path: ${{env.SRCDIR}}
go-version: ${{matrix.go}}

- name: build
run: |
cd $SRCDIR
make all
run: make all

- name: test
run: |
cd $SRCDIR
./test.sh
run: ./test.sh

send-coverage:
staticcheck:
runs-on: ubuntu-20.04
timeout-minutes: 30
container: "golang:1.16-bullseye"
env:
GOPATH: "${{github.workspace}}/go"
SRCDIR: "go/src/github.com/nsqio/nsq"

steps:
- uses: actions/checkout@v2

- uses: dominikh/staticcheck-action@v1.2.0
with:
path: ${{env.SRCDIR}}
version: "2022.1.3"
install-go: false

code-coverage:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2

- name: Install goveralls
- name: install goveralls
run: go install github.com/mattn/goveralls@latest

- name: Send coverage
- name: send coverage
env:
COVERALLS_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: |
cd $SRCDIR
./coverage.sh --coveralls
run: ./coverage.sh --coveralls
2 changes: 1 addition & 1 deletion apps/nsq_tail/nsq_tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {
cfg.MaxInFlight = *maxInFlight

consumers := []*nsq.Consumer{}
for i := 0; i < len(topics); i += 1 {
for i := 0; i < len(topics); i++ {
log.Printf("Adding consumer for topic: %s\n", topics[i])

consumer, err := nsq.NewConsumer(topics[i], *channel, cfg)
Expand Down
2 changes: 1 addition & 1 deletion apps/nsq_to_file/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func computeFilenameFormat(opts *Options, topic string) (string, error) {

cff := opts.FilenameFormat
if opts.GZIP || opts.RotateSize > 0 || opts.RotateInterval > 0 || opts.WorkDir != opts.OutputDir {
if strings.Index(cff, "<REV>") == -1 {
if !strings.Contains(cff, "<REV>") {
return "", errors.New("missing <REV> in --filename-format when gzip, rotation, or work dir enabled")
}
} else {
Expand Down
10 changes: 0 additions & 10 deletions apps/nsq_to_file/nsq_to_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ import (
"github.com/nsqio/nsq/internal/version"
)

func hasArg(s string) bool {
argExist := false
flag.Visit(func(f *flag.Flag) {
if f.Name == s {
argExist = true
}
})
return argExist
}

func flagSet() *flag.FlagSet {
fs := flag.NewFlagSet("nsqd", flag.ExitOnError)

Expand Down
19 changes: 4 additions & 15 deletions apps/nsq_to_http/nsq_to_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (p *PostPublisher) Publish(addr string, msg []byte) error {
if err != nil {
return err
}
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
Expand All @@ -147,7 +146,7 @@ func (p *GetPublisher) Publish(addr string, msg []byte) error {
if err != nil {
return err
}
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()

if resp.StatusCode != 200 {
Expand All @@ -156,16 +155,6 @@ func (p *GetPublisher) Publish(addr string, msg []byte) error {
return nil
}

func hasArg(s string) bool {
argExist := false
flag.Visit(func(f *flag.Flag) {
if f.Name == s {
argExist = true
}
})
return argExist
}

func main() {
var publisher Publisher
var addresses app.StringArray
Expand Down Expand Up @@ -306,12 +295,12 @@ func parseCustomHeaders(strs []string) (map[string]string, error) {
for _, s := range strs {
sp := strings.SplitN(s, ":", 2)
if len(sp) != 2 {
return nil, fmt.Errorf("Invalid headers: %q", s)
return nil, fmt.Errorf("invalid header: %q", s)
}
key := strings.TrimSpace(sp[0])
val := strings.TrimSpace(sp[1])
if key == "" || val == "" {
return nil, fmt.Errorf("Invalid headers: %q", s)
return nil, fmt.Errorf("invalid header: %q", s)
}
parsedHeaders[key] = val

Expand Down
10 changes: 0 additions & 10 deletions apps/nsq_to_nsq/nsq_to_nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,6 @@ func (ph *PublishHandler) HandleMessage(m *nsq.Message, destinationTopic string)
return nil
}

func hasArg(s string) bool {
argExist := false
flag.Visit(func(f *flag.Flag) {
if f.Name == s {
argExist = true
}
})
return argExist
}

func main() {
var selectedMode int

Expand Down
2 changes: 1 addition & 1 deletion apps/nsqadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func nsqadminFlagSet(opts *nsqadmin.Options) *flag.FlagSet {
flagSet.String("http-client-tls-key", "", "path to key file for the HTTP client")

flagSet.String("allow-config-from-cidr", opts.AllowConfigFromCIDR, "A CIDR from which to allow HTTP requests to the /config endpoint")
flagSet.String("acl-http-header", opts.AclHttpHeader, "HTTP header to check for authenticated admin users")
flagSet.String("acl-http-header", opts.ACLHTTPHeader, "HTTP header to check for authenticated admin users")

nsqlookupdHTTPAddresses := app.StringArray{}
flagSet.Var(&nsqlookupdHTTPAddresses, "lookupd-http-address", "lookupd HTTP address (may be given multiple times)")
Expand Down
1 change: 0 additions & 1 deletion apps/nsqd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var tlsVersionTable = []struct {
val uint16
str string
}{
{tls.VersionSSL30, "ssl3.0"},
{tls.VersionTLS10, "tls1.0"},
{tls.VersionTLS11, "tls1.1"},
{tls.VersionTLS12, "tls1.2"},
Expand Down
2 changes: 1 addition & 1 deletion apps/to_nsq/to_nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
}
log.Printf("Throttling messages rate to max:%d/second", *rate)
// every tick increase the number of messages we can send
for _ = range time.Tick(interval) {
for range time.Tick(interval) {
n := atomic.AddInt64(&balance, 1)
// if we build up more than 1s of capacity just bound to that
if n > int64(*rate) {
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_reader/bench_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
d := t.Sub(time.Now())
d := time.Until(t)
log.Printf("sleeping until %s (%s)", t, d)
time.Sleep(d)
}
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_writer/bench_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
d := t.Sub(time.Now())
d := time.Until(t)
log.Printf("sleeping until %s (%s)", t, d)
time.Sleep(d)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nsqio/nsq

go 1.16
go 1.17

require (
github.com/BurntSushi/toml v0.4.1
Expand Down
15 changes: 8 additions & 7 deletions internal/auth/authorizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth
import (
"errors"
"fmt"
"log"
"math/rand"
"net/url"
"regexp"
Expand Down Expand Up @@ -72,26 +71,28 @@ func (a *State) IsAllowed(topic, channel string) bool {
}

func (a *State) IsExpired() bool {
if a.Expires.Before(time.Now()) {
return true
}
return false
return a.Expires.Before(time.Now())
}

func QueryAnyAuthd(authd []string, remoteIP string, tlsEnabled bool, commonName string, authSecret string,
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
var retErr error
start := rand.Int()
n := len(authd)
for i := 0; i < n; i++ {
a := authd[(i+start)%n]
authState, err := QueryAuthd(a, remoteIP, tlsEnabled, commonName, authSecret, connectTimeout, requestTimeout)
if err != nil {
log.Printf("Error: failed auth against %s %s", a, err)
es := fmt.Sprintf("failed to auth against %s - %s", a, err)
if retErr != nil {
es = fmt.Sprintf("%s; %s", retErr, es)
}
retErr = errors.New(es)
continue
}
return authState, nil
}
return nil, errors.New("Unable to access auth server")
return nil, retErr
}

func QueryAuthd(authd string, remoteIP string, tlsEnabled bool, commonName string, authSecret string,
Expand Down
16 changes: 8 additions & 8 deletions internal/clusterinfo/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *ClusterInfo) GetLookupdTopics(lookupdHTTPAddrs []string) ([]string, err
wg.Wait()

if len(errs) == len(lookupdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqlookupd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqlookupd: %s", ErrList(errs))
}

topics = stringy.Uniq(topics)
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *ClusterInfo) GetLookupdTopicChannels(topic string, lookupdHTTPAddrs []s
wg.Wait()

if len(errs) == len(lookupdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqlookupd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqlookupd: %s", ErrList(errs))
}

channels = stringy.Uniq(channels)
Expand Down Expand Up @@ -219,7 +219,7 @@ func (c *ClusterInfo) GetLookupdProducers(lookupdHTTPAddrs []string) (Producers,
wg.Wait()

if len(errs) == len(lookupdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqlookupd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqlookupd: %s", ErrList(errs))
}

for _, producer := range producersByAddr {
Expand Down Expand Up @@ -280,7 +280,7 @@ func (c *ClusterInfo) GetLookupdTopicProducers(topic string, lookupdHTTPAddrs []
wg.Wait()

if len(errs) == len(lookupdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqlookupd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqlookupd: %s", ErrList(errs))
}
if len(errs) > 0 {
return producers, ErrList(errs)
Expand Down Expand Up @@ -328,7 +328,7 @@ func (c *ClusterInfo) GetNSQDTopics(nsqdHTTPAddrs []string) ([]string, error) {
wg.Wait()

if len(errs) == len(nsqdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqd: %s", ErrList(errs))
}

sort.Strings(topics)
Expand Down Expand Up @@ -415,7 +415,7 @@ func (c *ClusterInfo) GetNSQDProducers(nsqdHTTPAddrs []string) (Producers, error
wg.Wait()

if len(errs) == len(nsqdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqd: %s", ErrList(errs))
}
if len(errs) > 0 {
return producers, ErrList(errs)
Expand Down Expand Up @@ -519,7 +519,7 @@ func (c *ClusterInfo) GetNSQDTopicProducers(topic string, nsqdHTTPAddrs []string
wg.Wait()

if len(errs) == len(nsqdHTTPAddrs) {
return nil, fmt.Errorf("Failed to query any nsqd: %s", ErrList(errs))
return nil, fmt.Errorf("failed to query any nsqd: %s", ErrList(errs))
}
if len(errs) > 0 {
return producers, ErrList(errs)
Expand Down Expand Up @@ -616,7 +616,7 @@ func (c *ClusterInfo) GetNSQDStats(producers Producers,
wg.Wait()

if len(errs) == len(producers) {
return nil, nil, fmt.Errorf("Failed to query any nsqd: %s", ErrList(errs))
return nil, nil, fmt.Errorf("failed to query any nsqd: %s", ErrList(errs))
}

sort.Sort(TopicStatsByHost{topicStatsList})
Expand Down
8 changes: 4 additions & 4 deletions internal/clusterinfo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ func (s *ClientStats) UnmarshalJSON(b []byte) error {
return nil
}

func (c *ClientStats) HasUserAgent() bool {
return c.UserAgent != ""
func (s *ClientStats) HasUserAgent() bool {
return s.UserAgent != ""
}

func (c *ClientStats) HasSampleRate() bool {
return c.SampleRate > 0
func (s *ClientStats) HasSampleRate() bool {
return s.SampleRate > 0
}

type ChannelStatsList []*ChannelStats
Expand Down
6 changes: 3 additions & 3 deletions internal/http_api/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -61,7 +61,7 @@ retry:
return err
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -100,7 +100,7 @@ retry:
return err
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
Expand Down
Loading