chore: update linter.

This commit is contained in:
Ludovic Fernandez 2020-11-06 09:26:03 +01:00 committed by GitHub
parent f5b290b093
commit 267d0b7b5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 68 additions and 38 deletions

View file

@ -54,7 +54,10 @@
"nestif", # Too many false-positive. "nestif", # Too many false-positive.
"noctx", # Too strict "noctx", # Too strict
"exhaustive", # Too strict "exhaustive", # Too strict
"nlreturn", # Too strict "nlreturn", # Not relevant
"wrapcheck", # Too strict
"tparallel", # Not relevant
"exhaustivestruct", # Not relevant
] ]
[issues] [issues]

View file

@ -19,7 +19,7 @@ RUN mkdir -p /usr/local/bin \
&& chmod +x /usr/local/bin/go-bindata && chmod +x /usr/local/bin/go-bindata
# Download golangci-lint binary to bin folder in $GOPATH # Download golangci-lint binary to bin folder in $GOPATH
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.31.0 RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.32.2
# Download misspell binary to bin folder in $GOPATH # Download misspell binary to bin folder in $GOPATH
RUN curl -sfL https://raw.githubusercontent.com/client9/misspell/master/install-misspell.sh | bash -s -- -b $GOPATH/bin v0.3.4 RUN curl -sfL https://raw.githubusercontent.com/client9/misspell/master/install-misspell.sh | bash -s -- -b $GOPATH/bin v0.3.4

1
go.sum
View file

@ -1108,6 +1108,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=

View file

@ -137,7 +137,7 @@ func RotateFile() error {
} }
if err := OpenFile(logFilePath); err != nil { if err := OpenFile(logFilePath); err != nil {
return fmt.Errorf("error opening log file: %s", err) return fmt.Errorf("error opening log file: %w", err)
} }
return nil return nil

View file

@ -2,6 +2,7 @@ package metrics
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"sort" "sort"
"strings" "strings"
@ -74,12 +75,15 @@ func RegisterPrometheus(ctx context.Context, config *types.Prometheus) Registry
standardRegistry := initStandardRegistry(config) standardRegistry := initStandardRegistry(config)
if err := promRegistry.Register(stdprometheus.NewProcessCollector(stdprometheus.ProcessCollectorOpts{})); err != nil { if err := promRegistry.Register(stdprometheus.NewProcessCollector(stdprometheus.ProcessCollectorOpts{})); err != nil {
if _, ok := err.(stdprometheus.AlreadyRegisteredError); !ok { var arErr stdprometheus.AlreadyRegisteredError
if !errors.As(err, &arErr) {
log.FromContext(ctx).Warn("ProcessCollector is already registered") log.FromContext(ctx).Warn("ProcessCollector is already registered")
} }
} }
if err := promRegistry.Register(stdprometheus.NewGoCollector()); err != nil { if err := promRegistry.Register(stdprometheus.NewGoCollector()); err != nil {
if _, ok := err.(stdprometheus.AlreadyRegisteredError); !ok { var arErr stdprometheus.AlreadyRegisteredError
if !errors.As(err, &arErr) {
log.FromContext(ctx).Warn("GoCollector is already registered") log.FromContext(ctx).Warn("GoCollector is already registered")
} }
} }
@ -212,15 +216,21 @@ func initStandardRegistry(config *types.Prometheus) Registry {
} }
func registerPromState(ctx context.Context) bool { func registerPromState(ctx context.Context) bool {
if err := promRegistry.Register(promState); err != nil { err := promRegistry.Register(promState)
logger := log.FromContext(ctx) if err == nil {
if _, ok := err.(stdprometheus.AlreadyRegisteredError); !ok { return true
logger.Errorf("Unable to register Traefik to Prometheus: %v", err)
return false
}
logger.Debug("Prometheus collector already registered.")
} }
return true
logger := log.FromContext(ctx)
var arErr stdprometheus.AlreadyRegisteredError
if errors.As(err, &arErr) {
logger.Debug("Prometheus collector already registered.")
return true
}
logger.Errorf("Unable to register Traefik to Prometheus: %v", err)
return false
} }
// OnConfigurationUpdate receives the current configuration from Traefik. // OnConfigurationUpdate receives the current configuration from Traefik.

View file

@ -2,6 +2,7 @@ package auth
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
@ -124,7 +125,7 @@ func (fa *forwardAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
redirectURL, err := forwardResponse.Location() redirectURL, err := forwardResponse.Location()
if err != nil { if err != nil {
if err != http.ErrNoLocation { if !errors.Is(err, http.ErrNoLocation) {
logMessage := fmt.Sprintf("Error reading response location header %s. Cause: %s", fa.address, err) logMessage := fmt.Sprintf("Error reading response location header %s. Cause: %s", fa.address, err)
logger.Debug(logMessage) logger.Debug(logMessage)
tracing.SetErrorWithEvent(req, logMessage) tracing.SetErrorWithEvent(req, logMessage)

View file

@ -54,5 +54,6 @@ func recoverFunc(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
// https://github.com/golang/go/blob/a0d6420d8be2ae7164797051ec74fa2a2df466a1/src/net/http/server.go#L1761-L1775 // https://github.com/golang/go/blob/a0d6420d8be2ae7164797051ec74fa2a2df466a1/src/net/http/server.go#L1761-L1775
// https://github.com/golang/go/blob/c33153f7b416c03983324b3e8f869ce1116d84bc/src/net/http/httputil/reverseproxy.go#L284 // https://github.com/golang/go/blob/c33153f7b416c03983324b3e8f869ce1116d84bc/src/net/http/httputil/reverseproxy.go#L284
func shouldLogPanic(panicValue interface{}) bool { func shouldLogPanic(panicValue interface{}) bool {
//nolint:errorlint // false-positive because panicValue is an interface.
return panicValue != nil && panicValue != http.ErrAbortHandler return panicValue != nil && panicValue != http.ErrAbortHandler
} }

View file

@ -2,6 +2,7 @@ package docker
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -307,7 +308,7 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
startStopHandle(event) startStopHandle(event)
} }
case err := <-errc: case err := <-errc:
if err == io.EOF { if errors.Is(err, io.EOF) {
logger.Debug("Provider event stream closed") logger.Debug("Provider event stream closed")
} }
return err return err

View file

@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"errors"
"os" "os"
"os/signal" "os/signal"
"time" "time"
@ -85,9 +86,9 @@ func (s *Server) Close() {
go func(ctx context.Context) { go func(ctx context.Context) {
<-ctx.Done() <-ctx.Done()
if ctx.Err() == context.Canceled { if errors.Is(ctx.Err(), context.Canceled) {
return return
} else if ctx.Err() == context.DeadlineExceeded { } else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
panic("Timeout while stopping traefik, killing instance ✝") panic("Timeout while stopping traefik, killing instance ✝")
} }
}(ctx) }(ctx)

View file

@ -174,11 +174,15 @@ func (e *TCPEntryPoint) Start(ctx context.Context) {
conn, err := e.listener.Accept() conn, err := e.listener.Accept()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Temporary() {
continue continue
} }
e.httpServer.Forwarder.errChan <- err e.httpServer.Forwarder.errChan <- err
e.httpsServer.Forwarder.errChan <- err e.httpsServer.Forwarder.errChan <- err
return return
} }
@ -232,7 +236,7 @@ func (e *TCPEntryPoint) Shutdown(ctx context.Context) {
if err == nil { if err == nil {
return return
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
logger.Debugf("Server failed to shutdown within deadline because: %s", err) logger.Debugf("Server failed to shutdown within deadline because: %s", err)
if err = server.Close(); err != nil { if err = server.Close(); err != nil {
logger.Error(err) logger.Error(err)
@ -263,7 +267,7 @@ func (e *TCPEntryPoint) Shutdown(ctx context.Context) {
if err == nil { if err == nil {
return return
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
logger.Debugf("Server failed to shutdown before deadline because: %s", err) logger.Debugf("Server failed to shutdown before deadline because: %s", err)
} }
e.tracker.Close() e.tracker.Close()

View file

@ -46,7 +46,7 @@ func TestShutdownTCP(t *testing.T) {
for { for {
_, err := http.ReadRequest(bufio.NewReader(conn)) _, err := http.ReadRequest(bufio.NewReader(conn))
if err == io.EOF || (err != nil && strings.HasSuffix(err.Error(), "use of closed network connection")) { if errors.Is(err, io.EOF) || (err != nil && strings.HasSuffix(err.Error(), "use of closed network connection")) {
return return
} }
require.NoError(t, err) require.NoError(t, err)

View file

@ -81,13 +81,13 @@ func (m *Mirroring) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
logger := log.FromContext(req.Context()) logger := log.FromContext(req.Context())
rr, bytesRead, err := newReusableRequest(req, m.maxBodySize) rr, bytesRead, err := newReusableRequest(req, m.maxBodySize)
if err != nil && err != errBodyTooLarge { if err != nil && !errors.Is(err, errBodyTooLarge) {
http.Error(rw, http.StatusText(http.StatusInternalServerError)+ http.Error(rw, http.StatusText(http.StatusInternalServerError)+
fmt.Sprintf("error creating reusable request: %v", err), http.StatusInternalServerError) fmt.Sprintf("error creating reusable request: %v", err), http.StatusInternalServerError)
return return
} }
if err == errBodyTooLarge { if errors.Is(err, errBodyTooLarge) {
req.Body = ioutil.NopCloser(io.MultiReader(bytes.NewReader(bytesRead), req.Body)) req.Body = ioutil.NopCloser(io.MultiReader(bytes.NewReader(bytesRead), req.Body))
m.handler.ServeHTTP(rw, req) m.handler.ServeHTTP(rw, req)
logger.Debugf("no mirroring, request body larger than allowed size") logger.Debugf("no mirroring, request body larger than allowed size")
@ -196,13 +196,13 @@ func newReusableRequest(req *http.Request, maxBodySize int64) (*reusableRequest,
// the request body is larger than what we allow for the mirrors. // the request body is larger than what we allow for the mirrors.
body := make([]byte, maxBodySize+1) body := make([]byte, maxBodySize+1)
n, err := io.ReadFull(req.Body, body) n, err := io.ReadFull(req.Body, body)
if err != nil && err != io.ErrUnexpectedEOF { if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
return nil, nil, err return nil, nil, err
} }
// we got an ErrUnexpectedEOF, which means there was less than maxBodySize data to read, // we got an ErrUnexpectedEOF, which means there was less than maxBodySize data to read,
// which permits us sending also to all the mirrors later. // which permits us sending also to all the mirrors later.
if err == io.ErrUnexpectedEOF { if errors.Is(err, io.ErrUnexpectedEOF) {
return &reusableRequest{ return &reusableRequest{
req: req, req: req,
body: body[:n], body: body[:n],

View file

@ -2,6 +2,7 @@ package wrr
import ( import (
"container/heap" "container/heap"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"sync" "sync"
@ -105,7 +106,7 @@ func (b *Balancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if b.stickyCookie != nil { if b.stickyCookie != nil {
cookie, err := req.Cookie(b.stickyCookie.name) cookie, err := req.Cookie(b.stickyCookie.name)
if err != nil && err != http.ErrNoCookie { if err != nil && !errors.Is(err, http.ErrNoCookie) {
log.WithoutContext().Warnf("Error while reading cookie: %v", err) log.WithoutContext().Warnf("Error while reading cookie: %v", err)
} }

View file

@ -2,6 +2,7 @@ package service
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -83,13 +84,14 @@ func buildProxy(passHostHeader *bool, responseForwarding *dynamic.ResponseForwar
statusCode := http.StatusInternalServerError statusCode := http.StatusInternalServerError
switch { switch {
case err == io.EOF: case errors.Is(err, io.EOF):
statusCode = http.StatusBadGateway statusCode = http.StatusBadGateway
case err == context.Canceled: case errors.Is(err, context.Canceled):
statusCode = StatusClientClosedRequest statusCode = StatusClientClosedRequest
default: default:
if e, ok := err.(net.Error); ok { var netErr net.Error
if e.Timeout() { if errors.As(err, &netErr) {
if netErr.Timeout() {
statusCode = http.StatusGatewayTimeout statusCode = http.StatusGatewayTimeout
} else { } else {
statusCode = http.StatusBadGateway statusCode = http.StatusBadGateway

View file

@ -3,6 +3,7 @@ package service
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -49,12 +50,13 @@ func TestWebSocketTCPClose(t *testing.T) {
withPath("/ws"), withPath("/ws"),
).open() ).open()
require.NoError(t, err) require.NoError(t, err)
conn.Close() conn.Close()
serverErr := <-errChan serverErr := <-errChan
wsErr, ok := serverErr.(*gorillawebsocket.CloseError) var wsErr *gorillawebsocket.CloseError
assert.Equal(t, true, ok) require.True(t, errors.As(serverErr, &wsErr))
assert.Equal(t, 1006, wsErr.Code) assert.Equal(t, 1006, wsErr.Code)
} }
@ -119,7 +121,7 @@ func TestWebSocketPingPong(t *testing.T) {
_, _, err = conn.ReadMessage() _, _, err = conn.ReadMessage()
if err != goodErr { if !errors.Is(err, goodErr) {
require.NoError(t, err) require.NoError(t, err)
} }
} }

View file

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"errors"
"io" "io"
"net" "net"
"net/http" "net/http"
@ -197,10 +198,11 @@ func (c *Conn) Read(p []byte) (n int, err error) {
func clientHelloServerName(br *bufio.Reader) (string, bool, string, error) { func clientHelloServerName(br *bufio.Reader) (string, bool, string, error) {
hdr, err := br.Peek(1) hdr, err := br.Peek(1)
if err != nil { if err != nil {
opErr, ok := err.(*net.OpError) var opErr *net.OpError
if err != io.EOF && (!ok || !opErr.Timeout()) { if !errors.Is(err, io.EOF) && (!errors.As(err, &opErr) || opErr.Timeout()) {
log.WithoutContext().Debugf("Error while Peeking first byte: %s", err) log.WithoutContext().Debugf("Error while Peeking first byte: %s", err)
} }
return "", false, "", err return "", false, "", err
} }

View file

@ -1,6 +1,7 @@
package udp package udp
import ( import (
"errors"
"io" "io"
"net" "net"
"testing" "testing"
@ -24,7 +25,7 @@ func TestConsecutiveWrites(t *testing.T) {
go func() { go func() {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err == errClosedListener { if errors.Is(err, errClosedListener) {
return return
} }
require.NoError(t, err) require.NoError(t, err)
@ -86,7 +87,7 @@ func TestListenNotBlocking(t *testing.T) {
go func() { go func() {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err == errClosedListener { if errors.Is(err, errClosedListener) {
return return
} }
require.NoError(t, err) require.NoError(t, err)
@ -183,7 +184,7 @@ func testTimeout(t *testing.T, withRead bool) {
go func() { go func() {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err == errClosedListener { if errors.Is(err, errClosedListener) {
return return
} }
require.NoError(t, err) require.NoError(t, err)