From 4891fc5304e3ab9e23e9258a39736a3726b626c2 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Tue, 25 Mar 2025 14:40:55 +0900 Subject: [PATCH] Update golangci-lint to v2 (#1980) --- .github/workflows/lint.yml | 4 +- .golangci.yml | 129 ++++++++++------------ examples/client/client.go | 10 +- examples/fileserver/fileserver.go | 12 +- examples/host_client/hostclient.go | 2 +- examples/letsencrypt/letsencryptserver.go | 4 +- examples/multidomain/multidomain.go | 4 +- fasthttpproxy/dialer.go | 9 +- fasthttpproxy/doc.go | 2 + header.go | 5 +- pprofhandler/pprof.go | 1 + prefork/prefork.go | 1 + server.go | 2 +- workerpool.go | 5 +- 14 files changed, 90 insertions(+), 100 deletions(-) create mode 100644 fasthttpproxy/doc.go diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7f81b76..5e1122b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,7 +23,7 @@ jobs: go-version: 1.24.x - run: go version - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v7 with: - version: v1.64.5 + version: v2.0.1 args: --verbose diff --git a/.golangci.yml b/.golangci.yml index dc8c148..984d3e4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,15 +1,9 @@ -# This file contains configuration options for golangci-lint. -# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml - -run: - # Timeout for analysis. - timeout: 5m - +version: "2" linters: - enable-all: true + default: all disable: - - cyclop - copyloopvar + - cyclop - depguard - dupl - err113 @@ -17,15 +11,16 @@ linters: - errorlint - exhaustive - exhaustruct + - forbidigo - forcetypeassert - funlen - gochecknoglobals - gocognit - goconst - gocyclo - - goerr113 - - gomnd + - godox - gosec + - gosmopolitan - inamedparam - intrange - ireturn @@ -46,65 +41,61 @@ linters: - varnamelen - wrapcheck - wsl - - # Deprecated linters - - deadcode - - exhaustivestruct - - exportloopref - - execinquery - - golint - - ifshort - - interfacer - - maligned - - nosnakecase - - scopelint - - structcheck - - varcheck - -linters-settings: - revive: - # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md + settings: + gocritic: + disabled-checks: + - deferInLoop + - importShadow + - sloppyReassign + - unnamedResult + - whyNoLint + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + govet: + disable: + - fieldalignment + - shadow + enable-all: true + lll: + line-length: 130 + revive: + rules: + - name: indent-error-flow + - name: use-any + staticcheck: + checks: + - -ST1000 + - all + exclusions: + generated: lax + presets: + - common-false-positives + - legacy + - std-error-handling rules: - - name: indent-error-flow - - name: use-any - lll: - line-length: 130 - stylecheck: - checks: [ - "all", - "-ST1000", # at least one file in a package should have a package comment - ] - gocritic: - enabled-tags: - - diagnostic - - experimental - - opinionated - - performance - - style - disabled-checks: - - deferInLoop - - importShadow - - sloppyReassign - - unnamedResult - - whyNoLint - govet: - enable-all: true - disable: - - fieldalignment - - shadow - + - linters: + - lll + path: _test\.go + paths: + - third_party$ + - builtin$ + - examples$ issues: - # Show all issues from a linter. max-issues-per-linter: 0 - - # Show all issues with the same text. max-same-issues: 0 - - include: - - EXC0011 # include issues about comments from `stylecheck` - - exclude-rules: - # Exclude some linters from running on tests files. - - path: _test\.go - linters: - - lll +formatters: + enable: + - gci + - gofmt + - gofumpt + - goimports + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/examples/client/client.go b/examples/client/client.go index 42ef57a..890738c 100644 --- a/examples/client/client.go +++ b/examples/client/client.go @@ -13,13 +13,13 @@ import ( "github.com/valyala/fasthttp" ) -var headerContentTypeJson = []byte("application/json") +var headerContentTypeJSON = []byte("application/json") var client *fasthttp.Client type Entity struct { - Name string - Id int + Name string `json:"name"` + ID int `json:"id"` } func main() { @@ -66,12 +66,12 @@ func sendPostRequest() { reqEntity := &Entity{ Name: "New entity", } - reqEntityBytes, _ := json.Marshal(reqEntity) + reqEntityBytes, _ := json.Marshal(reqEntity) //nolint:errchkjson req := fasthttp.AcquireRequest() req.SetRequestURI("http://localhost:8080/") req.Header.SetMethod(fasthttp.MethodPost) - req.Header.SetContentTypeBytes(headerContentTypeJson) + req.Header.SetContentTypeBytes(headerContentTypeJSON) req.SetBodyRaw(reqEntityBytes) resp := fasthttp.AcquireResponse() diff --git a/examples/fileserver/fileserver.go b/examples/fileserver/fileserver.go index 2a915be..aa97349 100644 --- a/examples/fileserver/fileserver.go +++ b/examples/fileserver/fileserver.go @@ -15,14 +15,14 @@ import ( var ( addr = flag.String("addr", "localhost:8080", "TCP address to listen to") - addrTLS = flag.String("addrTLS", "", "TCP address to listen to TLS (aka SSL or HTTPS) requests. Leave empty for disabling TLS") + addrTLS = flag.String("addrTLS", "", "TCP address to listen to TLS (aka SSL or HTTPS) requests. Leave empty for disabling TLS") //nolint:lll byteRange = flag.Bool("byteRange", false, "Enables byte range requests if set to true") certFile = flag.String("certFile", "./ssl-cert.pem", "Path to TLS certificate file") compress = flag.Bool("compress", false, "Enables transparent response compression if set to true") dir = flag.String("dir", "/usr/share/nginx/html", "Directory to serve static files from") generateIndexPages = flag.Bool("generateIndexPages", true, "Whether to generate directory index pages") keyFile = flag.String("keyFile", "./ssl-cert.key", "Path to TLS key file") - vhost = flag.Bool("vhost", false, "Enables virtual hosting by prepending the requested path with the requested hostname") + vhost = flag.Bool("vhost", false, "Enables virtual hosting by prepending the requested path with the requested hostname") //nolint:lll ) func main() { @@ -59,7 +59,7 @@ func main() { } // Start HTTP server. - if len(*addr) > 0 { + if *addr != "" { log.Printf("Starting HTTP server on %q", *addr) go func() { if err := fasthttp.ListenAndServe(*addr, requestHandler); err != nil { @@ -69,7 +69,7 @@ func main() { } // Start HTTPS server. - if len(*addrTLS) > 0 { + if *addrTLS != "" { log.Printf("Starting HTTPS server on %q", *addrTLS) go func() { if err := fasthttp.ListenAndServeTLS(*addrTLS, *certFile, *keyFile, requestHandler); err != nil { @@ -106,10 +106,10 @@ func updateFSCounters(ctx *fasthttp.RequestCtx) { // Various counters - see https://pkg.go.dev/expvar for details. var ( - // Counter for total number of fs calls + // Counter for total number of fs calls. fsCalls = expvar.NewInt("fsCalls") - // Counters for various response status codes + // Counters for various response status codes. fsOKResponses = expvar.NewInt("fsOKResponses") fsNotModifiedResponses = expvar.NewInt("fsNotModifiedResponses") fsNotFoundResponses = expvar.NewInt("fsNotFoundResponses") diff --git a/examples/host_client/hostclient.go b/examples/host_client/hostclient.go index 997abd4..156e3f9 100644 --- a/examples/host_client/hostclient.go +++ b/examples/host_client/hostclient.go @@ -10,7 +10,7 @@ import ( func main() { // Get URI from a pool url := fasthttp.AcquireURI() - url.Parse(nil, []byte("http://localhost:8080/")) + url.Parse(nil, []byte("http://localhost:8080/")) //nolint:errcheck url.SetUsername("Aladdin") url.SetPassword("Open Sesame") diff --git a/examples/letsencrypt/letsencryptserver.go b/examples/letsencrypt/letsencryptserver.go index c8e8362..4450e8c 100644 --- a/examples/letsencrypt/letsencryptserver.go +++ b/examples/letsencrypt/letsencryptserver.go @@ -33,9 +33,9 @@ func main() { panic(err) } - lnTls := tls.NewListener(ln, cfg) + lnTLS := tls.NewListener(ln, cfg) - if err := fasthttp.Serve(lnTls, requestHandler); err != nil { + if err := fasthttp.Serve(lnTLS, requestHandler); err != nil { panic(err) } } diff --git a/examples/multidomain/multidomain.go b/examples/multidomain/multidomain.go index 9b5ca15..ee0a5d3 100644 --- a/examples/multidomain/multidomain.go +++ b/examples/multidomain/multidomain.go @@ -37,7 +37,7 @@ func main() { panic(err) } domains["localhost:8080"] = func(ctx *fasthttp.RequestCtx) { - ctx.WriteString("You are accessing to localhost:8080\n") + ctx.WriteString("You are accessing to localhost:8080\n") //nolint:errcheck } err = server.AppendCertEmbed(cert, priv) @@ -51,7 +51,7 @@ func main() { panic(err) } domains["127.0.0.1:8080"] = func(ctx *fasthttp.RequestCtx) { - ctx.WriteString("You are accessing to 127.0.0.1:8080\n") + ctx.WriteString("You are accessing to 127.0.0.1:8080\n") //nolint:errcheck } err = server.AppendCertEmbed(cert, priv) diff --git a/fasthttpproxy/dialer.go b/fasthttpproxy/dialer.go index 9846491..52dd32b 100644 --- a/fasthttpproxy/dialer.go +++ b/fasthttpproxy/dialer.go @@ -79,10 +79,7 @@ func (d *Dialer) GetDialFunc(useEnv bool) (dialFunc fasthttp.DialFunc, err error if useEnv { config = httpproxy.FromEnvironment() } - proxyURLIsSame := false - if config.HTTPSProxy == config.HTTPProxy && config.NoProxy == "" { - proxyURLIsSame = true - } + proxyURLIsSame := config.HTTPSProxy == config.HTTPProxy && config.NoProxy == "" network := "tcp4" if d.DialDualStack { network = "tcp" @@ -158,13 +155,13 @@ func (d *Dialer) GetDialFunc(useEnv bool) (dialFunc fasthttp.DialFunc, err error func (d *Dialer) Dial(network, addr string) (conn net.Conn, err error) { if network == "tcp4" { if d.Timeout > 0 { - return d.TCPDialer.DialTimeout(addr, d.Timeout) + return d.DialTimeout(addr, d.Timeout) } return d.TCPDialer.Dial(addr) } if network == "tcp" { if d.Timeout > 0 { - return d.TCPDialer.DialDualStackTimeout(addr, d.Timeout) + return d.DialDualStackTimeout(addr, d.Timeout) } return d.TCPDialer.DialDualStack(addr) } diff --git a/fasthttpproxy/doc.go b/fasthttpproxy/doc.go new file mode 100644 index 0000000..d014191 --- /dev/null +++ b/fasthttpproxy/doc.go @@ -0,0 +1,2 @@ +// Package fasthttpproxy provides SOCKS5 and HTTP proxy support for fasthttp. +package fasthttpproxy diff --git a/header.go b/header.go index bd5e3c1..d2850b1 100644 --- a/header.go +++ b/header.go @@ -3331,10 +3331,7 @@ func (s *headerScanner) next() bool { s.err = errNeedMore return false } - for { - if n+1 >= len(s.b) { - break - } + for n+1 < len(s.b) { if s.b[n+1] != ' ' && s.b[n+1] != '\t' { break } diff --git a/pprofhandler/pprof.go b/pprofhandler/pprof.go index f372a80..7081c69 100644 --- a/pprofhandler/pprof.go +++ b/pprofhandler/pprof.go @@ -1,3 +1,4 @@ +// Package pprofhandler provides a fasthttp handler similar to net/http/pprof. package pprofhandler import ( diff --git a/prefork/prefork.go b/prefork/prefork.go index df38e65..46d308a 100644 --- a/prefork/prefork.go +++ b/prefork/prefork.go @@ -1,3 +1,4 @@ +// Package prefork provides a way to prefork a fasthttp server. package prefork import ( diff --git a/server.go b/server.go index 3f871cf..2e95701 100644 --- a/server.go +++ b/server.go @@ -1157,7 +1157,7 @@ var ( // NetHttpFormValueFunc gives consistent behavior with net/http. // POST and PUT body parameters take precedence over URL query string values. // - //nolint:stylecheck // backwards compatibility + //nolint:staticcheck // backwards compatibility NetHttpFormValueFunc = func(ctx *RequestCtx, key string) []byte { v := ctx.PostArgs().Peek(key) if len(v) > 0 { diff --git a/workerpool.go b/workerpool.go index 9ecd948..438bd29 100644 --- a/workerpool.go +++ b/workerpool.go @@ -224,12 +224,13 @@ func (wp *workerPool) workerFunc(ch *workerChan) { if err = wp.WorkerFunc(c); err != nil && err != errHijacked { errStr := err.Error() - if wp.LogAllErrors || !(strings.Contains(errStr, "broken pipe") || + shouldIgnore := strings.Contains(errStr, "broken pipe") || strings.Contains(errStr, "reset by peer") || strings.Contains(errStr, "request headers: small read buffer") || strings.Contains(errStr, "unexpected EOF") || strings.Contains(errStr, "i/o timeout") || - errors.Is(err, ErrBadTrailer)) { + errors.Is(err, ErrBadTrailer) + if wp.LogAllErrors || !shouldIgnore { wp.Logger.Printf("error when serving connection %q<->%q: %v", c.LocalAddr(), c.RemoteAddr(), err) } }