From f863b07f3d3b01de53fa946dd94038c489183a22 Mon Sep 17 00:00:00 2001 From: Daisuke Maki Date: Wed, 18 Sep 2024 17:06:25 +0900 Subject: [PATCH] Appease linter --- .golangci.yml | 53 ++++++++++++++++++++++++++++++++++++++---- buffered.go | 18 ++++++++------ errors.go | 13 ++++++----- fluent.go | 1 + fluent_example_test.go | 5 ++-- fluent_test.go | 20 ++++++---------- go.mod | 2 +- interface.go | 3 ++- minion.go | 3 ++- ping.go | 1 + pool.go | 1 + unbuffered.go | 19 ++++++++------- 12 files changed, 94 insertions(+), 45 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 3a8ad73..f7f07de 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,19 +1,21 @@ run: - skip-dirs: internal linters-settings: govet: enable-all: true disable: - shadow + - fieldalignment linters: enable-all: true disable: + - cyclop + - depguard - dupl - exhaustive - - exhaustivestruct - errorlint + - err113 - funlen - gci - gochecknoglobals @@ -23,30 +25,71 @@ linters: - gocyclo - godot - godox - - goerr113 - gofumpt - gomnd - gosec + - govet + - inamedparam # oh, sod off + - ireturn # No, I _LIKE_ returning interfaces - lll + - maintidx # Do this in code review - makezero + - mnd - nakedret - nestif - nlreturn + - nonamedreturns # visit this back later - paralleltest + - perfsprint + - tagliatelle + - testifylint # TODO: revisit when we have the chance - testpackage - - thelper - - unconvert + - thelper # Tests are fine + - varnamelen # Short names are ok - wrapcheck - wsl issues: exclude-rules: + # not needed + - path: /*.go + text: "ST1003: should not use underscores in package names" + linters: + - stylecheck + - path: /*.go + text: "don't use an underscore in package name" + linters: + - revive + - path: /*.go + linters: + - contextcheck + - exhaustruct + - path: /main.go + linters: + - errcheck + - path: internal/codegen/codegen.go + linters: + - errcheck + - path: internal/jwxtest/jwxtest.go + linters: + - errcheck + - errchkjson + - forcetypeassert - path: /*_test.go linters: - errcheck + - errchkjson + - forcetypeassert - path: /*_example_test.go linters: - forbidigo + - path: cmd/jwx/jwx.go + linters: + - forbidigo + - path: /*_test.go + text: "var-naming: " + litners: + - revive # Maximum issues count per one linter. Set to 0 to disable. Default is 50. max-issues-per-linter: 0 diff --git a/buffered.go b/buffered.go index 8dd5ae3..296420d 100644 --- a/buffered.go +++ b/buffered.go @@ -38,6 +38,7 @@ func NewBuffered(options ...Option) (client *Buffered, err error) { ctx, cancel := context.WithCancel(context.Background()) var subsecond bool + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identSubSecond{}: @@ -63,17 +64,16 @@ func NewBuffered(options ...Option) (client *Buffered, err error) { // If you would like to specify options to `Post()`, you may pass them at the end of // the method. Currently you can use the following: // -// fluent.WithContext: specify context.Context to use -// fluent.WithTimestamp: allows you to set arbitrary timestamp values -// fluent.WithSyncAppend: allows you to verify if the append was successful +// fluent.WithContext: specify context.Context to use +// fluent.WithTimestamp: allows you to set arbitrary timestamp values +// fluent.WithSyncAppend: allows you to verify if the append was successful // // If fluent.WithSyncAppend is provide and is true, the following errors // may be returned: // -// 1. If the current underlying pending buffer is is not large enough to -// hold this new data, an error will be returned -// 2. If the marshaling into msgpack/json failed, it is returned -// +// 1. If the current underlying pending buffer is not large enough to +// hold this new data, an error will be returned +// 2. If the marshaling into msgpack/json failed, it is returned func (c *Buffered) Post(tag string, v interface{}, options ...Option) (err error) { if pdebug.Enabled { g := pdebug.Marker("fluent.Buffered.Post").BindError(&err) @@ -91,6 +91,7 @@ func (c *Buffered) Post(tag string, v interface{}, options ...Option) (err error var subsecond = c.subsecond var t time.Time var ctx = context.Background() + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identTimestamp{}: @@ -103,6 +104,7 @@ func (c *Buffered) Post(tag string, v interface{}, options ...Option) (err error if pdebug.Enabled { pdebug.Printf("client: using user-supplied context") } + //nolint:fatcontext ctx = opt.Value().(context.Context) } } @@ -221,6 +223,7 @@ func (c *Buffered) Ping(tag string, record interface{}, options ...Option) (err var ctx = context.Background() var subsecond bool var t time.Time + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identSubSecond{}: @@ -231,6 +234,7 @@ func (c *Buffered) Ping(tag string, record interface{}, options ...Option) (err if pdebug.Enabled { pdebug.Printf("client: using user-supplied context") } + //nolint:fatcontext ctx = opt.Value().(context.Context) } } diff --git a/errors.go b/errors.go index b41dd33..0a2b1c8 100644 --- a/errors.go +++ b/errors.go @@ -1,7 +1,8 @@ package fluent -type bufferFullErr struct{} -type bufferFuller interface { +//nolint:errname +type errBufferFull struct{} +type errBufferFuller interface { BufferFull() bool } type causer interface { @@ -9,12 +10,12 @@ type causer interface { } // Just need one instance -var bufferFullErrInstance bufferFullErr +var errBufferFullInstance errBufferFull // IsBufferFull returns true if the error is a BufferFull error func IsBufferFull(e error) bool { for e != nil { - if berr, ok := e.(bufferFuller); ok { + if berr, ok := e.(errBufferFuller); ok { return berr.BufferFull() } @@ -25,10 +26,10 @@ func IsBufferFull(e error) bool { return false } -func (e *bufferFullErr) BufferFull() bool { +func (e *errBufferFull) BufferFull() bool { return true } -func (e *bufferFullErr) Error() string { +func (e *errBufferFull) Error() string { return `buffer full` } diff --git a/fluent.go b/fluent.go index bf06331..3be5c82 100644 --- a/fluent.go +++ b/fluent.go @@ -9,6 +9,7 @@ package fluent // respectively. func New(options ...Option) (Client, error) { var buffered = true + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identBuffered{}: diff --git a/fluent_example_test.go b/fluent_example_test.go index 82f22cf..4e54fbd 100644 --- a/fluent_example_test.go +++ b/fluent_example_test.go @@ -55,7 +55,7 @@ func ExamplePing() { return } - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // Goroutine to wait for errors @@ -75,6 +75,7 @@ func ExamplePing() { }() go fluent.Ping(ctx, client, "ping", "hostname", fluent.WithPingResultChan(errorCh)) - // Do what you need with your main program... + + // OUTPUT: } diff --git a/fluent_test.go b/fluent_test.go index 4849684..00a45a1 100644 --- a/fluent_test.go +++ b/fluent_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "os" "path/filepath" @@ -35,7 +34,7 @@ type server struct { } func newServer(useJSON bool) (*server, error) { - dir, err := ioutil.TempDir("", "sock-") + dir, err := os.MkdirTemp("", "sock-") if err != nil { return nil, errors.Wrap(err, `failed to create temporary directory`) } @@ -252,12 +251,11 @@ func (s *server) Run(ctx context.Context) { func TestConnectOnStart(t *testing.T) { for _, buffered := range []bool{true, false} { - buffered := buffered t.Run(fmt.Sprintf("failure case, buffered=%t", buffered), func(t *testing.T) { // find a port that is not available (this may be timing dependent) var dialer net.Dialer - var port int = 22412 - for i := 0; i < 1000; i++ { + port := 22412 + for range 1000 { ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) conn, err := dialer.DialContext(ctx, `net`, fmt.Sprintf(`127.0.0.1:%d`, port)) cancel() @@ -295,7 +293,6 @@ func TestConnectOnStart(t *testing.T) { <-s.Ready() for _, buffered := range []bool{true, false} { - buffered := buffered t.Run(fmt.Sprintf("normal case, buffered=%t", buffered), func(t *testing.T) { client, err := fluent.New( fluent.WithNetwork(s.Network), @@ -490,7 +487,6 @@ func (msg *badmsgpack) EncodeMsgpack(_ *msgpack.Encoder) error { func TestPostSync(t *testing.T) { for _, syncAppend := range []bool{true, false} { - syncAppend := syncAppend t.Run("sync="+strconv.FormatBool(syncAppend), func(t *testing.T) { s, err := newServer(false) if !assert.NoError(t, err, "newServer should succeed") { @@ -548,8 +544,8 @@ func TestPostSync(t *testing.T) { } type Payload struct { - Foo string `msgpack:"foo" json:"foo"` - Bar string `msgpack:"bar" json:"bar"` + Foo string `json:"foo" msgpack:"foo"` + Bar string `json:"bar" msgpack:"bar"` } func TestPostRoundtrip(t *testing.T) { @@ -562,7 +558,6 @@ func TestPostRoundtrip(t *testing.T) { } for _, buffered := range []bool{true, false} { - buffered := buffered t.Run(fmt.Sprintf("buffered=%t", buffered), func(t *testing.T) { var options []fluent.Option if !buffered { @@ -672,13 +667,12 @@ func TestPostRoundtrip(t *testing.T) { func TestPing(t *testing.T) { for _, buffered := range []bool{true, false} { - buffered := buffered t.Run(fmt.Sprintf("buffered=%t", buffered), func(t *testing.T) { t.Run("Ping with no server", func(t *testing.T) { // find a port that is not available (this may be timing dependent) var dialer net.Dialer - var port int = 22412 - for i := 0; i < 1000; i++ { + port := 22412 + for range 1000 { ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) conn, err := dialer.DialContext(ctx, `net`, fmt.Sprintf(`127.0.0.1:%d`, port)) cancel() diff --git a/go.mod b/go.mod index 835ab1a..504cf40 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/lestrrat-go/fluent-client -go 1.23.1 +go 1.22.7 require ( github.com/lestrrat-go/backoff/v2 v2.0.8 diff --git a/interface.go b/interface.go index 9114cd3..74eb3b6 100644 --- a/interface.go +++ b/interface.go @@ -21,9 +21,10 @@ type Client interface { Shutdown(context.Context) error } -//nolint:maligned // Buffered is a Client that buffers incoming messages, and sends them // asynchrnously when it can. +// +//nolint:maligned type Buffered struct { closed bool minionCancel func() diff --git a/minion.go b/minion.go index c040084..131bf07 100644 --- a/minion.go +++ b/minion.go @@ -90,6 +90,7 @@ func newMinion(options ...Option) (*minion, error) { var writeQueueSize = 64 var connectOnStart bool + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identNetwork{}: @@ -306,7 +307,7 @@ func (m *minion) appendMessage(msg *Message) { if pdebug.Enabled { pdebug.Printf("background reader: replying error to client") } - msg.replyCh <- &bufferFullErrInstance + msg.replyCh <- &errBufferFullInstance } return } diff --git a/ping.go b/ping.go index 4361a0e..705020a 100644 --- a/ping.go +++ b/ping.go @@ -16,6 +16,7 @@ func Ping(ctx context.Context, client Client, tag string, record interface{}, op var interval = 5 * time.Minute var replyCh chan error + //nolint:forcetypeassert for _, option := range options { switch option.Ident() { case identPingInterval{}: diff --git a/pool.go b/pool.go index 8d2ac70..0e2f348 100644 --- a/pool.go +++ b/pool.go @@ -11,6 +11,7 @@ func allocMessage() interface{} { } func getMessage() *Message { + //nolint:forcetypeassert return msgpool.Get().(*Message) } diff --git a/unbuffered.go b/unbuffered.go index 8aa2355..be26af0 100644 --- a/unbuffered.go +++ b/unbuffered.go @@ -14,13 +14,13 @@ import ( // buffered client, an unbuffered client handles the Post() method // synchronously, and does not attempt to buffer the payload. // -// * fluent.WithAddress -// * fluent.WithDialTimeout -// * fluent.WithMarshaler -// * fluent.WithMaxConnAttempts -// * fluent.WithNetwork -// * fluent.WithSubSecond -// * fluent.WithTagPrefix +// - fluent.WithAddress +// - fluent.WithDialTimeout +// - fluent.WithMarshaler +// - fluent.WithMaxConnAttempts +// - fluent.WithNetwork +// - fluent.WithSubSecond +// - fluent.WithTagPrefix // // Please see their respective documentation for details. func NewUnbuffered(options ...Option) (client *Unbuffered, err error) { @@ -39,6 +39,7 @@ func NewUnbuffered(options ...Option) (client *Unbuffered, err error) { } var connectOnStart bool + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identAddress{}: @@ -128,8 +129,7 @@ func (c *Unbuffered) serialize(msg *Message) ([]byte, error) { // If you would like to specify options to `Post()`, you may pass them at the // end of the method. Currently you can use the following: // -// fluent.WithTimestamp: allows you to set arbitrary timestamp values -// +// fluent.WithTimestamp: allows you to set arbitrary timestamp values func (c *Unbuffered) Post(tag string, v interface{}, options ...Option) (err error) { if pdebug.Enabled { g := pdebug.Marker("fluent.Unbuffered.Post").BindError(&err) @@ -137,6 +137,7 @@ func (c *Unbuffered) Post(tag string, v interface{}, options ...Option) (err err } var t time.Time + //nolint:forcetypeassert for _, opt := range options { switch opt.Ident() { case identTimestamp{}: