From 2c4adddb634a9b90e2bc2ddb66ce540d7b098ab8 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Sun, 27 Jun 2021 23:14:48 +0200 Subject: [PATCH] Fix go-vet warning about mutex copying --- receiver/base.go | 28 +++++++++++++++++----------- receiver/receiver.go | 24 +++++++++++++----------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/receiver/base.go b/receiver/base.go index 595b745e..005c8676 100644 --- a/receiver/base.go +++ b/receiver/base.go @@ -28,18 +28,18 @@ type Base struct { pastDropped uint64 // atomic tooLongDropped uint64 // atomic } - droppedList [droppedListSize]string - droppedListNext int - droppedListMu sync.Mutex - parseThreads int - dropFutureSeconds uint32 - dropPastSeconds uint32 - dropTooLongLimit uint16 + droppedList [droppedListSize]string + droppedListNext int + droppedListMu sync.Mutex + parseThreads int + dropFutureSeconds uint32 + dropPastSeconds uint32 + dropTooLongLimit uint16 readTimeoutSeconds uint32 - writeChan chan *RowBinary.WriteBuffer - logger *zap.Logger - Tags tags.TagConfig - concatCharacter string + writeChan chan *RowBinary.WriteBuffer + logger *zap.Logger + Tags tags.TagConfig + concatCharacter string } func NewBase(logger *zap.Logger, config tags.TagConfig) Base { @@ -56,6 +56,12 @@ func sendInt64Gauge(send func(metric string, value float64), metric string, valu send(metric, float64(atomic.LoadInt64(value))) } +func (base *Base) applyOptions(opts ...Option) { + for _, applyOption := range opts { + applyOption(base) + } +} + func (base *Base) isDrop(nowTime uint32, metricTime uint32) bool { if base.dropFutureSeconds != 0 && (metricTime > (nowTime + base.dropFutureSeconds)) { atomic.AddUint64(&base.stat.futureDropped, 1) diff --git a/receiver/receiver.go b/receiver/receiver.go index fcab776b..f7005c30 100644 --- a/receiver/receiver.go +++ b/receiver/receiver.go @@ -97,11 +97,7 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { return nil, err } - base := NewBase(zapwriter.Logger(strings.Replace(u.Scheme, "+", "_", -1)), config) - - for _, optApply := range opts { - optApply(&base) - } + logger := zapwriter.Logger(strings.Replace(u.Scheme, "+", "_", -1)) if u.Scheme == "tcp" { addr, err := net.ResolveTCPAddr("tcp", u.Host) @@ -110,9 +106,10 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &TCP{ - Base: base, + Base: NewBase(logger, config), parseChan: make(chan *Buffer), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err @@ -128,9 +125,10 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &Pickle{ - Base: base, + Base: NewBase(logger, config), parseChan: make(chan []byte), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err @@ -146,9 +144,10 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &UDP{ - Base: base, + Base: NewBase(logger, config), parseChan: make(chan *Buffer), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err @@ -164,8 +163,9 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &GRPC{ - Base: base, + Base: NewBase(logger, config), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err @@ -181,8 +181,9 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &PrometheusRemoteWrite{ - Base: base, + Base: NewBase(logger, config), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err @@ -198,8 +199,9 @@ func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) { } r := &TelegrafHttpJson{ - Base: base, + Base: NewBase(logger, config), } + r.applyOptions(opts...) if err = r.Listen(addr); err != nil { return nil, err