Skip to content

Commit

Permalink
Appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Sep 19, 2024
1 parent b00227d commit f863b07
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 45 deletions.
53 changes: 48 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
18 changes: 11 additions & 7 deletions buffered.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand All @@ -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)
Expand All @@ -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{}:
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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{}:
Expand All @@ -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)
}
}
Expand Down
13 changes: 7 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package fluent

type bufferFullErr struct{}
type bufferFuller interface {
//nolint:errname
type errBufferFull struct{}
type errBufferFuller interface {
BufferFull() bool
}
type causer interface {
Cause() error
}

// 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()
}

Expand All @@ -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`
}
1 change: 1 addition & 0 deletions fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand Down
5 changes: 3 additions & 2 deletions fluent_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
}
20 changes: 7 additions & 13 deletions fluent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -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`)
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
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/lestrrat-go/fluent-client

go 1.23.1
go 1.22.7

require (
github.com/lestrrat-go/backoff/v2 v2.0.8
Expand Down
3 changes: 2 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion minion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand Down
1 change: 1 addition & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func allocMessage() interface{} {
}

func getMessage() *Message {
//nolint:forcetypeassert
return msgpool.Get().(*Message)
}

Expand Down
Loading

0 comments on commit f863b07

Please sign in to comment.