From 74d3162e42d8d3ac3ee32e0fe60c7d7564b2c025 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 18 Nov 2024 15:39:54 +0100 Subject: [PATCH] fix: lint issues --- .golangci.yml | 5 ++--- net/gotsrpc/errors.go | 6 +++--- net/http/cookie/domainprovider.go | 4 ++-- net/http/roundtripware/circuitbreaker_test.go | 8 ++++---- net/stream/jetstream/stream.go | 2 +- server.go | 1 - server_test.go | 8 ++++---- service/goroutine.go | 3 +-- telemetry/log.go | 2 +- 9 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 04c7f69..05aaa2d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -111,14 +111,13 @@ linters: - bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false] #- containedctx # containedctx is a linter that detects struct contained context.Context field [fast: false, auto-fix: false] - contextcheck # check whether the function uses a non-inherited context [fast: false, auto-fix: false] - #- copyloopvar # (go >= 1.22) copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false] + - copyloopvar # (go >= 1.22) copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false] - decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false] - durationcheck # check for two durations multiplied together [fast: false, auto-fix: false] - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false] - errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false] - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false] - exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false] - - exportloopref # checks for pointers to enclosing loop variables [fast: false, auto-fix: false] #- forbidigo # Forbids identifiers [fast: false, auto-fix: false] - forcetypeassert # finds forced type assertions [fast: true, auto-fix: false] - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false] @@ -136,7 +135,7 @@ linters: - grouper # Analyze expression groups. [fast: true, auto-fix: false] - importas # Enforces consistent import aliases [fast: false, auto-fix: false] #- inamedparam # reports interfaces with unnamed method parameters [fast: true, auto-fix: false] - #- intrange # (go >= 1.22) intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false] + - intrange # (go >= 1.22) intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false] #- loggercheck # (logrlint) Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). [fast: false, auto-fix: false] - makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false] - misspell # Finds commonly misspelled English words [fast: true, auto-fix: true] diff --git a/net/gotsrpc/errors.go b/net/gotsrpc/errors.go index 46219ce..882f97d 100644 --- a/net/gotsrpc/errors.go +++ b/net/gotsrpc/errors.go @@ -5,9 +5,9 @@ type Error string // Common errors const ( - ErrorNotFound Error = "notFound" //nolint:errname - ErrorForbidden Error = "forbidden" //nolint:errname - ErrorPermissionDenied Error = "permissionDenied" //nolint:errname + ErrorNotFound Error = "notFound" + ErrorForbidden Error = "forbidden" + ErrorPermissionDenied Error = "permissionDenied" ) // NewError returns a pointer error diff --git a/net/http/cookie/domainprovider.go b/net/http/cookie/domainprovider.go index f6fffc9..dbe1216 100644 --- a/net/http/cookie/domainprovider.go +++ b/net/http/cookie/domainprovider.go @@ -48,7 +48,7 @@ func NewDomainProvider(opts ...DomainProviderOption) DomainProvider { return func(r *http.Request) (string, error) { domain := keelhttp.GetRequestDomain(r) - if options.Domains == nil || len(options.Domains) == 0 { + if len(options.Domains) == 0 { return domain, nil } @@ -58,7 +58,7 @@ func NewDomainProvider(opts ...DomainProviderOption) DomainProvider { } } - if options.Domains != nil && len(options.Domains) > 0 { + if len(options.Domains) > 0 { for _, value := range options.Domains { // foo.com = foo.com // foo.com = *.foo.com diff --git a/net/http/roundtripware/circuitbreaker_test.go b/net/http/roundtripware/circuitbreaker_test.go index 36804b4..af53ddb 100644 --- a/net/http/roundtripware/circuitbreaker_test.go +++ b/net/http/roundtripware/circuitbreaker_test.go @@ -295,7 +295,7 @@ func TestCircuitBreakerInterval(t *testing.T) { ) // send exactly 3 requests (lower than the maximum amount of allowed consecutive failures) - for i := 0; i < 3; i++ { + for range 3 { req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, svr.URL, nil) require.NoError(t, err) resp, err := client.Do(req) @@ -310,7 +310,7 @@ func TestCircuitBreakerInterval(t *testing.T) { // now we should be able to send 3 more requests without triggering the circuit breaker (last request should finally // trigger the circuit breaker, but the error will not yet be a circuitbreaker error) - for i := 0; i <= 3; i++ { + for range 4 { req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, svr.URL, nil) require.NoError(t, err) resp, err := client.Do(req) @@ -362,7 +362,7 @@ func TestCircuitBreakerIgnore(t *testing.T) { // send 4 requests (higher than the maximum amount of allowed consecutive failures), but they are ignored // -> circuit breaker should remain open - for i := 0; i < 4; i++ { + for range 4 { req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, svr.URL, nil) require.NoError(t, err) resp, err := client.Do(req) @@ -394,7 +394,7 @@ func TestCircuitBreakerTimeout(t *testing.T) { // send 4 requests (more than the maximum amount of allowed consecutive failures) // -> circuit breaker should change to open state - for i := 0; i < 4; i++ { + for range 4 { ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) req, err := http.NewRequestWithContext(ctx, http.MethodGet, svr.URL, nil) diff --git a/net/stream/jetstream/stream.go b/net/stream/jetstream/stream.go index 129d330..0eb6c56 100644 --- a/net/stream/jetstream/stream.go +++ b/net/stream/jetstream/stream.go @@ -190,7 +190,7 @@ func (s *Stream) initNatsOptions() { s.l.Error("nats disconnected error", log.FError(err)) var errRetry error - for i := 0; i < s.reconnectMaxRetries; i++ { + for range s.reconnectMaxRetries { errRetry = s.connect() if errRetry != nil { s.l.Error("nats reconnect failed", log.FError(errRetry)) diff --git a/server.go b/server.go index 551ee0c..a035638 100644 --- a/server.go +++ b/server.go @@ -405,7 +405,6 @@ func (s *Server) Readme() string { func (s *Server) startService(services ...Service) { c := make(chan struct{}, 1) for _, value := range services { - value := value s.g.Go(func() error { c <- struct{}{} if err := value.Start(s.ctx); errors.Is(err, http.ErrServerClosed) { diff --git a/server_test.go b/server_test.go index 0499cac..b28740e 100644 --- a/server_test.go +++ b/server_test.go @@ -98,7 +98,7 @@ func (s *KeelTestSuite) TestServiceHTTPZap() { s.Run("default", func() { if statusCode, body, err := s.httpGet("http://localhost:9100/log"); s.NoError(err) { s.Equal(http.StatusOK, statusCode) - s.Equal(`{"level":"info","disableCaller":true,"disableStacktrace":true}`, body) + s.JSONEq(`{"level":"info","disableCaller":true,"disableStacktrace":true}`, body) } if statusCode, _, err := s.httpGet("http://localhost:55000/log/info"); s.NoError(err) { s.Equal(http.StatusOK, statusCode) @@ -111,7 +111,7 @@ func (s *KeelTestSuite) TestServiceHTTPZap() { s.Run("set debug level", func() { if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"level":"debug"}`); s.NoError(err) { s.Equal(http.StatusOK, statusCode) - s.Equal(`{"level":"debug","disableCaller":true,"disableStacktrace":true}`, body) + s.JSONEq(`{"level":"debug","disableCaller":true,"disableStacktrace":true}`, body) } if statusCode, _, err := s.httpGet("http://localhost:55000/log/info"); s.NoError(err) { s.Equal(http.StatusOK, statusCode) @@ -124,7 +124,7 @@ func (s *KeelTestSuite) TestServiceHTTPZap() { s.Run("enable caller", func() { if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"disableCaller":false}`); s.NoError(err) { s.Equal(http.StatusOK, statusCode) - s.Equal(`{"level":"debug","disableCaller":false,"disableStacktrace":true}`, body) + s.JSONEq(`{"level":"debug","disableCaller":false,"disableStacktrace":true}`, body) } if statusCode, _, err := s.httpGet("http://localhost:55000/log/error"); s.NoError(err) { s.Equal(http.StatusOK, statusCode) @@ -134,7 +134,7 @@ func (s *KeelTestSuite) TestServiceHTTPZap() { s.Run("enable stacktrace", func() { if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"disableStacktrace":false}`); s.NoError(err) { s.Equal(http.StatusOK, statusCode) - s.Equal(`{"level":"debug","disableCaller":false,"disableStacktrace":false}`, body) + s.JSONEq(`{"level":"debug","disableCaller":false,"disableStacktrace":false}`, body) } if statusCode, _, err := s.httpGet("http://localhost:55000/log/error"); s.NoError(err) { s.Equal(http.StatusOK, statusCode) diff --git a/service/goroutine.go b/service/goroutine.go index ef895b7..ec4ae8b 100644 --- a/service/goroutine.go +++ b/service/goroutine.go @@ -87,8 +87,7 @@ func (s *GoRoutine) Start(ctx context.Context) error { s.cancelLock.Lock() s.cancel = cancel s.cancelLock.Unlock() - for i := 0; i < s.parallel; i++ { - i := i + for i := range s.parallel { l := log.WithAttributes(s.l, log.KeelServiceInstKey.Int(i)) s.wg.Go(func() error { return s.handler(ctx, l) diff --git a/telemetry/log.go b/telemetry/log.go index 55a5ef5..ba7433b 100644 --- a/telemetry/log.go +++ b/telemetry/log.go @@ -22,7 +22,7 @@ func (l Logger) Init(info logr.RuntimeInfo) { } func (l Logger) Enabled(level int) bool { - return log.AtomicLevel().Enabled(zapcore.Level(-1 * level)) + return log.AtomicLevel().Enabled(zapcore.Level(-1 * level)) //nolint:gosec } func (l Logger) Info(level int, msg string, keysAndValues ...interface{}) {