fix lint issues (#16)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-13 01:46:16 +03:00 committed by GitHub
parent fd5ed64729
commit abb9937787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 108 additions and 79 deletions

View File

@ -59,22 +59,6 @@ type Service struct {
Services []*register.Service 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 // Encode encodes an endpoint to endpoint metadata
func Encode(e *Endpoint) map[string]string { func Encode(e *Endpoint) map[string]string {
if e == nil { if e == nil {

View File

@ -36,10 +36,8 @@ func TestRequestToProto(t *testing.T) {
for k, v := range d.Header { for k, v := range d.Header {
if val, ok := p.Header[k]; !ok { if val, ok := p.Header[k]; !ok {
t.Fatalf("Expected header %s", k) t.Fatalf("Expected header %s", k)
} else { } else if val.Values[0] != v[0] {
if val.Values[0] != v[0] { t.Fatalf("Expected val %s, got %s", val.Values[0], v[0])
t.Fatalf("Expected val %s, got %s", val.Values[0], v[0])
}
} }
} }
} }

View File

@ -129,10 +129,8 @@ func (m *memoryBroker) Publish(ctx context.Context, topic string, msg *Message,
} }
if eh != nil { if eh != nil {
eh(p) eh(p)
} else { } else if m.opts.Logger.V(logger.ErrorLevel) {
if m.opts.Logger.V(logger.ErrorLevel) { m.opts.Logger.Error(m.opts.Context, err.Error())
m.opts.Logger.Error(m.opts.Context, err.Error())
}
} }
continue continue
} }

View File

@ -107,7 +107,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
if err != nil { if err != nil {
return err return err
} }
value.Set(reflect.ValueOf(float64(v))) value.Set(reflect.ValueOf(v))
case reflect.Int: case reflect.Int:
v, err := strconv.ParseInt(val, 10, 0) v, err := strconv.ParseInt(val, 10, 0)
if err != nil { if err != nil {
@ -137,7 +137,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
if err != nil { if err != nil {
return err return err
} }
value.Set(reflect.ValueOf(int64(v))) value.Set(reflect.ValueOf(v))
case reflect.Uint: case reflect.Uint:
v, err := strconv.ParseUint(val, 10, 0) v, err := strconv.ParseUint(val, 10, 0)
if err != nil { if err != nil {
@ -167,7 +167,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
if err != nil { if err != nil {
return err return err
} }
value.Set(reflect.ValueOf(uint64(v))) value.Set(reflect.ValueOf(v))
} }
return nil return nil
} }

View File

@ -7,8 +7,7 @@ import (
// NoopMeter is an noop implementation of Meter // NoopMeter is an noop implementation of Meter
type noopMeter struct { type noopMeter struct {
opts Options opts Options
labels Labels
} }
// NewMeter returns a configured noop reporter: // NewMeter returns a configured noop reporter:
@ -39,27 +38,47 @@ func (r *noopMeter) Counter(name string, opts ...Option) Counter {
// FloatCounter implements the Meter interface // FloatCounter implements the Meter interface
func (r *noopMeter) FloatCounter(name string, opts ...Option) FloatCounter { 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 // Gauge implements the Meter interface
func (r *noopMeter) Gauge(name string, f func() float64, opts ...Option) Gauge { 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 // Summary implements the Meter interface
func (r *noopMeter) Summary(name string, opts ...Option) Summary { 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 // SummaryExt implements the Meter interface
func (r *noopMeter) SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary { 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 // Histogram implements the Meter interface
func (r *noopMeter) Histogram(name string, opts ...Option) Histogram { 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 // 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) { 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 { func (r *noopGauge) Get() float64 {
return 0 return 0
} }
type noopSummary struct{} type noopSummary struct {
labels Labels
}
func (r *noopSummary) Update(float64) { 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() { func (r *noopHistogram) Reset() {

View File

@ -88,7 +88,7 @@ func NewTransport(opts ...transport.Option) transport.Transport {
} }
// initialise // initialise
t.Init(opts...) //t.Init(opts...)
return t return t
} }

View File

@ -72,6 +72,7 @@ func NewOptions(opts ...Option) Options {
} }
for _, o := range opts { for _, o := range opts {
//nolint:errcheck
o(&options) o(&options)
} }

View File

@ -57,26 +57,23 @@ func (m *memory) ttlPrune() {
prune := time.NewTicker(ttlPruneTime) prune := time.NewTicker(ttlPruneTime)
defer prune.Stop() defer prune.Stop()
for { for range prune.C {
select { m.Lock()
case <-prune.C: for domain, services := range m.records {
m.Lock() for service, versions := range services {
for domain, services := range m.records { for version, record := range versions {
for service, versions := range services { for id, n := range record.Nodes {
for version, record := range versions { if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
for id, n := range record.Nodes { if m.opts.Logger.V(logger.DebugLevel) {
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL { m.opts.Logger.Debugf(m.opts.Context, "Register TTL expired for node %s of service %s", n.Id, service)
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)
} }
delete(m.records[domain][service][version].Nodes, id)
} }
} }
} }
} }
m.Unlock()
} }
m.Unlock()
} }
} }

View File

@ -36,7 +36,17 @@ type Route struct {
// Hash returns route hash sum. // Hash returns route hash sum.
func (r *Route) Hash() uint64 { func (r *Route) Hash() uint64 {
h := fnv.New64() h := fnv.New64()
h.Reset() //nolint:errcheck
h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Router + r.Link)) 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() return h.Sum64()
} }

View File

@ -22,6 +22,7 @@ func (r *random) Select(routes []string, opts ...selector.SelectOption) (selecto
} }
// select a random route from the slice // select a random route from the slice
//nolint:gosec
return routes[rand.Intn(len(routes))] return routes[rand.Intn(len(routes))]
}, nil }, nil
} }
@ -40,5 +41,5 @@ func (r *random) String() string {
// NewSelector returns a random selector // NewSelector returns a random selector
func NewSelector(opts ...selector.Option) selector.Selector { func NewSelector(opts ...selector.Option) selector.Selector {
return new(random) return &random{}
} }

View File

@ -216,10 +216,13 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
hdr := make(map[string]string, len(msg.Header)) hdr := make(map[string]string, len(msg.Header))
for k, v := range msg.Header { for k, v := range msg.Header {
if k == "Content-Type" {
continue
}
hdr[k] = v 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)) results := make(chan error, len(sb.handlers))

View File

@ -92,7 +92,9 @@ func (s *service) Init(opts ...Option) error {
var err error var err error
// process options // process options
for _, o := range opts { for _, o := range opts {
o(&s.opts) if err = o(&s.opts); err != nil {
return err
}
} }
for _, cfg := range s.opts.Configs { for _, cfg := range s.opts.Configs {

View File

@ -11,7 +11,9 @@ import (
func TestMemoryReInit(t *testing.T) { func TestMemoryReInit(t *testing.T) {
s := store.NewStore(store.Table("aaa")) 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 { if len(s.Options().Table) > 0 {
t.Error("Init didn't reinitialise the store") t.Error("Init didn't reinitialise the store")
} }
@ -19,25 +21,33 @@ func TestMemoryReInit(t *testing.T) {
func TestMemoryBasic(t *testing.T) { func TestMemoryBasic(t *testing.T) {
s := store.NewStore() s := store.NewStore()
s.Init() if err := s.Init(); err != nil {
t.Fatal(err)
}
basictest(s, t) basictest(s, t)
} }
func TestMemoryPrefix(t *testing.T) { func TestMemoryPrefix(t *testing.T) {
s := store.NewStore() 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) basictest(s, t)
} }
func TestMemoryNamespace(t *testing.T) { func TestMemoryNamespace(t *testing.T) {
s := store.NewStore() 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) basictest(s, t)
} }
func TestMemoryNamespacePrefix(t *testing.T) { func TestMemoryNamespacePrefix(t *testing.T) {
s := store.NewStore() 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) basictest(s, t)
} }
@ -53,10 +63,8 @@ func basictest(s store.Store, t *testing.T) {
var val []byte var val []byte
if err := s.Read(ctx, "Hello", &val); err != nil { if err := s.Read(ctx, "Hello", &val); err != nil {
t.Error(err) t.Error(err)
} else { } else if string(val) != "World" {
if string(val) != "World" { t.Errorf("Expected %s, got %s", "World", val)
t.Errorf("Expected %s, got %s", "World", val)
}
} }
time.Sleep(time.Millisecond * 200) time.Sleep(time.Millisecond * 200)
if err := s.Read(ctx, "Hello", &val); err != store.ErrNotFound { if err := s.Read(ctx, "Hello", &val); err != store.ErrNotFound {

View File

@ -185,6 +185,7 @@ metadata:
{{- end }} {{- end }}
` `
//nolint:gosec
var secretTmpl = ` var secretTmpl = `
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret

View File

@ -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) 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 // Handle each question
for _, q := range query.Question { for _, q := range query.Question {

View File

@ -243,7 +243,7 @@ func MergeMap(a interface{}, b map[string]interface{}) error {
if fva.Elem().Type().Kind() == reflect.Struct { if fva.Elem().Type().Kind() == reflect.Struct {
for i := 0; i < fva.Elem().NumField(); i++ { for i := 0; i < fva.Elem().NumField(); i++ {
field := fva.Elem().Field(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())) field.Set(reflect.New(field.Type().Elem()))
} }
} }

View File

@ -62,7 +62,7 @@ func (l literal) compile() []op {
} }
func (v variable) compile() []op { func (v variable) compile() []op {
var ops []op ops := make([]op, 0, len(v.segments))
for _, s := range v.segments { for _, s := range v.segments {
ops = append(ops, s.compile()...) ops = append(ops, s.compile()...)
} }
@ -78,11 +78,12 @@ func (v variable) compile() []op {
} }
func (t template) Compile() Template { func (t template) Compile() Template {
var rawOps []op rawOps := make([]op, 0, len(t.segments))
for _, s := range t.segments { for _, s := range t.segments {
rawOps = append(rawOps, s.compile()...) rawOps = append(rawOps, s.compile()...)
} }
//ops := make([]int, 0, len(rawOps))
var ( var (
ops []int ops []int
pool []string pool []string

View File

@ -36,7 +36,7 @@ func (r *request) Codec() codec.Codec {
} }
func (r *request) Header() metadata.Metadata { func (r *request) Header() metadata.Metadata {
md, _ := metadata.FromContext(r.context) md, _ := metadata.FromIncomingContext(r.context)
return md return md
} }

View File

@ -16,12 +16,9 @@ func (c *syncStore) syncManager() {
} }
}(i, tickerAggregator, ticker) }(i, tickerAggregator, ticker)
} }
for { for i := range tickerAggregator {
select { println(i.index, "ticked")
case i := <-tickerAggregator: c.processQueue(i.index)
println(i.index, "ticked")
c.processQueue(i.index)
}
} }
} }