From 4bf640cb15af50e8f1259f545a9adf670628dc5e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 13 Feb 2021 01:44:23 +0300 Subject: [PATCH] fix lint issues Signed-off-by: Vasiliy Tolstov --- api/api.go | 16 --------- api/handler/api/util_test.go | 6 ++-- broker/memory.go | 6 ++-- config/default.go | 6 ++-- meter/noop.go | 49 +++++++++++++++++++++------ network/tunnel/transport/transport.go | 2 +- options.go | 1 + register/memory.go | 25 ++++++-------- router/route.go | 14 ++++++-- selector/random/random.go | 3 +- server/subscriber.go | 7 ++-- service.go | 4 ++- store/memory_test.go | 26 +++++++++----- util/kubernetes/client/templates.go | 1 + util/mdns/server.go | 3 +- util/reflect/reflect.go | 2 +- util/router/compile.go | 5 +-- util/stream/stream.go | 2 +- util/sync/manager.go | 9 ++--- 19 files changed, 108 insertions(+), 79 deletions(-) diff --git a/api/api.go b/api/api.go index 6001428e..d475b689 100644 --- a/api/api.go +++ b/api/api.go @@ -59,22 +59,6 @@ type Service struct { Services []*register.Service } -func strip(s string) string { - return strings.TrimSpace(s) -} - -func slice(s string) []string { - var sl []string - - for _, p := range strings.Split(s, ",") { - if str := strip(p); len(str) > 0 { - sl = append(sl, strip(p)) - } - } - - return sl -} - // Encode encodes an endpoint to endpoint metadata func Encode(e *Endpoint) map[string]string { if e == nil { diff --git a/api/handler/api/util_test.go b/api/handler/api/util_test.go index 9350bcde..9ac69068 100644 --- a/api/handler/api/util_test.go +++ b/api/handler/api/util_test.go @@ -36,10 +36,8 @@ func TestRequestToProto(t *testing.T) { for k, v := range d.Header { if val, ok := p.Header[k]; !ok { t.Fatalf("Expected header %s", k) - } else { - if val.Values[0] != v[0] { - t.Fatalf("Expected val %s, got %s", val.Values[0], v[0]) - } + } else if val.Values[0] != v[0] { + t.Fatalf("Expected val %s, got %s", val.Values[0], v[0]) } } } diff --git a/broker/memory.go b/broker/memory.go index f6b8d82d..0a19169b 100644 --- a/broker/memory.go +++ b/broker/memory.go @@ -129,10 +129,8 @@ func (m *memoryBroker) Publish(ctx context.Context, topic string, msg *Message, } if eh != nil { eh(p) - } else { - if m.opts.Logger.V(logger.ErrorLevel) { - m.opts.Logger.Error(m.opts.Context, err.Error()) - } + } else if m.opts.Logger.V(logger.ErrorLevel) { + m.opts.Logger.Error(m.opts.Context, err.Error()) } continue } diff --git a/config/default.go b/config/default.go index 4800c238..e8dbd74e 100644 --- a/config/default.go +++ b/config/default.go @@ -107,7 +107,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val if err != nil { return err } - value.Set(reflect.ValueOf(float64(v))) + value.Set(reflect.ValueOf(v)) case reflect.Int: v, err := strconv.ParseInt(val, 10, 0) if err != nil { @@ -137,7 +137,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val if err != nil { return err } - value.Set(reflect.ValueOf(int64(v))) + value.Set(reflect.ValueOf(v)) case reflect.Uint: v, err := strconv.ParseUint(val, 10, 0) if err != nil { @@ -167,7 +167,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val if err != nil { return err } - value.Set(reflect.ValueOf(uint64(v))) + value.Set(reflect.ValueOf(v)) } return nil } diff --git a/meter/noop.go b/meter/noop.go index 61a8f386..383aac49 100644 --- a/meter/noop.go +++ b/meter/noop.go @@ -7,8 +7,7 @@ import ( // NoopMeter is an noop implementation of Meter type noopMeter struct { - opts Options - labels Labels + opts Options } // NewMeter returns a configured noop reporter: @@ -39,27 +38,47 @@ func (r *noopMeter) Counter(name string, opts ...Option) Counter { // FloatCounter implements the Meter interface func (r *noopMeter) FloatCounter(name string, opts ...Option) FloatCounter { - return &noopFloatCounter{} + options := Options{} + for _, o := range opts { + o(&options) + } + return &noopFloatCounter{labels: options.Labels} } // Gauge implements the Meter interface func (r *noopMeter) Gauge(name string, f func() float64, opts ...Option) Gauge { - return &noopGauge{} + options := Options{} + for _, o := range opts { + o(&options) + } + return &noopGauge{labels: options.Labels} } // Summary implements the Meter interface func (r *noopMeter) Summary(name string, opts ...Option) Summary { - return &noopSummary{} + options := Options{} + for _, o := range opts { + o(&options) + } + return &noopSummary{labels: options.Labels} } // SummaryExt implements the Meter interface func (r *noopMeter) SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary { - return &noopSummary{} + options := Options{} + for _, o := range opts { + o(&options) + } + return &noopSummary{labels: options.Labels} } // Histogram implements the Meter interface func (r *noopMeter) Histogram(name string, opts ...Option) Histogram { - return &noopHistogram{} + options := Options{} + for _, o := range opts { + o(&options) + } + return &noopHistogram{labels: options.Labels} } // Set implements the Meter interface @@ -111,7 +130,9 @@ func (r *noopCounter) Set(uint64) { } -type noopFloatCounter struct{} +type noopFloatCounter struct { + labels Labels +} func (r *noopFloatCounter) Add(float64) { @@ -129,13 +150,17 @@ func (r *noopFloatCounter) Sub(float64) { } -type noopGauge struct{} +type noopGauge struct { + labels Labels +} func (r *noopGauge) Get() float64 { return 0 } -type noopSummary struct{} +type noopSummary struct { + labels Labels +} func (r *noopSummary) Update(float64) { @@ -145,7 +170,9 @@ func (r *noopSummary) UpdateDuration(time.Time) { } -type noopHistogram struct{} +type noopHistogram struct { + labels Labels +} func (r *noopHistogram) Reset() { diff --git a/network/tunnel/transport/transport.go b/network/tunnel/transport/transport.go index 46cf21b7..d817471d 100644 --- a/network/tunnel/transport/transport.go +++ b/network/tunnel/transport/transport.go @@ -88,7 +88,7 @@ func NewTransport(opts ...transport.Option) transport.Transport { } // initialise - t.Init(opts...) + //t.Init(opts...) return t } diff --git a/options.go b/options.go index ceccf039..3fab544a 100644 --- a/options.go +++ b/options.go @@ -72,6 +72,7 @@ func NewOptions(opts ...Option) Options { } for _, o := range opts { + //nolint:errcheck o(&options) } diff --git a/register/memory.go b/register/memory.go index a9b27e00..ffd9ffa7 100644 --- a/register/memory.go +++ b/register/memory.go @@ -57,26 +57,23 @@ func (m *memory) ttlPrune() { prune := time.NewTicker(ttlPruneTime) defer prune.Stop() - for { - select { - case <-prune.C: - m.Lock() - for domain, services := range m.records { - for service, versions := range services { - for version, record := range versions { - for id, n := range record.Nodes { - if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL { - if m.opts.Logger.V(logger.DebugLevel) { - m.opts.Logger.Debugf(m.opts.Context, "Register TTL expired for node %s of service %s", n.Id, service) - } - delete(m.records[domain][service][version].Nodes, id) + for range prune.C { + m.Lock() + for domain, services := range m.records { + for service, versions := range services { + for version, record := range versions { + for id, n := range record.Nodes { + if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL { + if m.opts.Logger.V(logger.DebugLevel) { + m.opts.Logger.Debugf(m.opts.Context, "Register TTL expired for node %s of service %s", n.Id, service) } + delete(m.records[domain][service][version].Nodes, id) } } } } - m.Unlock() } + m.Unlock() } } diff --git a/router/route.go b/router/route.go index 50d535a8..9b66529b 100644 --- a/router/route.go +++ b/router/route.go @@ -36,7 +36,17 @@ type Route struct { // Hash returns route hash sum. func (r *Route) Hash() uint64 { h := fnv.New64() - h.Reset() - h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Router + r.Link)) + //nolint:errcheck + h.Write([]byte(r.Service)) + //nolint:errcheck + h.Write([]byte(r.Address)) + //nolint:errcheck + h.Write([]byte(r.Gateway)) + //nolint:errcheck + h.Write([]byte(r.Network)) + //nolint:errcheck + h.Write([]byte(r.Router)) + //nolint:errcheck + h.Write([]byte(r.Link)) return h.Sum64() } diff --git a/selector/random/random.go b/selector/random/random.go index b5d92970..cfcf0e58 100644 --- a/selector/random/random.go +++ b/selector/random/random.go @@ -22,6 +22,7 @@ func (r *random) Select(routes []string, opts ...selector.SelectOption) (selecto } // select a random route from the slice + //nolint:gosec return routes[rand.Intn(len(routes))] }, nil } @@ -40,5 +41,5 @@ func (r *random) String() string { // NewSelector returns a random selector func NewSelector(opts ...selector.Option) selector.Selector { - return new(random) + return &random{} } diff --git a/server/subscriber.go b/server/subscriber.go index d4685546..25b9b988 100644 --- a/server/subscriber.go +++ b/server/subscriber.go @@ -216,10 +216,13 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl hdr := make(map[string]string, len(msg.Header)) for k, v := range msg.Header { + if k == "Content-Type" { + continue + } hdr[k] = v } - delete(hdr, "Content-Type") - ctx := metadata.NewContext(sb.opts.Context, hdr) + + ctx := metadata.NewIncomingContext(sb.opts.Context, hdr) results := make(chan error, len(sb.handlers)) diff --git a/service.go b/service.go index 6480a0b4..1d0f5e97 100644 --- a/service.go +++ b/service.go @@ -92,7 +92,9 @@ func (s *service) Init(opts ...Option) error { var err error // process options for _, o := range opts { - o(&s.opts) + if err = o(&s.opts); err != nil { + return err + } } for _, cfg := range s.opts.Configs { diff --git a/store/memory_test.go b/store/memory_test.go index cc1fb3c6..b46a9867 100644 --- a/store/memory_test.go +++ b/store/memory_test.go @@ -11,7 +11,9 @@ import ( func TestMemoryReInit(t *testing.T) { s := store.NewStore(store.Table("aaa")) - s.Init(store.Table("")) + if err := s.Init(store.Table("")); err != nil { + t.Fatal(err) + } if len(s.Options().Table) > 0 { t.Error("Init didn't reinitialise the store") } @@ -19,25 +21,33 @@ func TestMemoryReInit(t *testing.T) { func TestMemoryBasic(t *testing.T) { s := store.NewStore() - s.Init() + if err := s.Init(); err != nil { + t.Fatal(err) + } basictest(s, t) } func TestMemoryPrefix(t *testing.T) { s := store.NewStore() - s.Init(store.Table("some-prefix")) + if err := s.Init(store.Table("some-prefix")); err != nil { + t.Fatal(err) + } basictest(s, t) } func TestMemoryNamespace(t *testing.T) { s := store.NewStore() - s.Init(store.Database("some-namespace")) + if err := s.Init(store.Database("some-namespace")); err != nil { + t.Fatal(err) + } basictest(s, t) } func TestMemoryNamespacePrefix(t *testing.T) { s := store.NewStore() - s.Init(store.Table("some-prefix"), store.Database("some-namespace")) + if err := s.Init(store.Table("some-prefix"), store.Database("some-namespace")); err != nil { + t.Fatal(err) + } basictest(s, t) } @@ -53,10 +63,8 @@ func basictest(s store.Store, t *testing.T) { var val []byte if err := s.Read(ctx, "Hello", &val); err != nil { t.Error(err) - } else { - if string(val) != "World" { - t.Errorf("Expected %s, got %s", "World", val) - } + } else if string(val) != "World" { + t.Errorf("Expected %s, got %s", "World", val) } time.Sleep(time.Millisecond * 200) if err := s.Read(ctx, "Hello", &val); err != store.ErrNotFound { diff --git a/util/kubernetes/client/templates.go b/util/kubernetes/client/templates.go index 334cb088..ee35b483 100644 --- a/util/kubernetes/client/templates.go +++ b/util/kubernetes/client/templates.go @@ -185,6 +185,7 @@ metadata: {{- end }} ` +//nolint:gosec var secretTmpl = ` apiVersion: v1 kind: Secret diff --git a/util/mdns/server.go b/util/mdns/server.go index 7cdfdd43..d4b532f8 100644 --- a/util/mdns/server.go +++ b/util/mdns/server.go @@ -248,7 +248,8 @@ func (s *Server) handleQuery(query *dns.Msg, from net.Addr) error { return fmt.Errorf("[ERR] mdns: support for DNS requests with high truncated bit not implemented: %v", *query) } - var unicastAnswer, multicastAnswer []dns.RR + unicastAnswer := make([]dns.RR, 0, len(query.Question)) + multicastAnswer := make([]dns.RR, 0, len(query.Question)) // Handle each question for _, q := range query.Question { diff --git a/util/reflect/reflect.go b/util/reflect/reflect.go index d3d648d9..7a5d60ab 100644 --- a/util/reflect/reflect.go +++ b/util/reflect/reflect.go @@ -243,7 +243,7 @@ func MergeMap(a interface{}, b map[string]interface{}) error { if fva.Elem().Type().Kind() == reflect.Struct { for i := 0; i < fva.Elem().NumField(); i++ { field := fva.Elem().Field(i) - if field.Type().Kind() == reflect.Ptr && field.IsNil() && fva.Elem().Type().Field(i).Anonymous == true { + if field.Type().Kind() == reflect.Ptr && field.IsNil() && fva.Elem().Type().Field(i).Anonymous { field.Set(reflect.New(field.Type().Elem())) } } diff --git a/util/router/compile.go b/util/router/compile.go index 606334a2..91d9f4ae 100644 --- a/util/router/compile.go +++ b/util/router/compile.go @@ -62,7 +62,7 @@ func (l literal) compile() []op { } func (v variable) compile() []op { - var ops []op + ops := make([]op, 0, len(v.segments)) for _, s := range v.segments { ops = append(ops, s.compile()...) } @@ -78,11 +78,12 @@ func (v variable) compile() []op { } func (t template) Compile() Template { - var rawOps []op + rawOps := make([]op, 0, len(t.segments)) for _, s := range t.segments { rawOps = append(rawOps, s.compile()...) } + //ops := make([]int, 0, len(rawOps)) var ( ops []int pool []string diff --git a/util/stream/stream.go b/util/stream/stream.go index 4476a10d..3536be99 100644 --- a/util/stream/stream.go +++ b/util/stream/stream.go @@ -36,7 +36,7 @@ func (r *request) Codec() codec.Codec { } func (r *request) Header() metadata.Metadata { - md, _ := metadata.FromContext(r.context) + md, _ := metadata.FromIncomingContext(r.context) return md } diff --git a/util/sync/manager.go b/util/sync/manager.go index f03f5a30..7de2afa3 100644 --- a/util/sync/manager.go +++ b/util/sync/manager.go @@ -16,12 +16,9 @@ func (c *syncStore) syncManager() { } }(i, tickerAggregator, ticker) } - for { - select { - case i := <-tickerAggregator: - println(i.index, "ticked") - c.processQueue(i.index) - } + for i := range tickerAggregator { + println(i.index, "ticked") + c.processQueue(i.index) } }