From 53bd5373d4891fe5b2a861b14786e3c119beba80 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 26 Apr 2021 01:07:20 +0300 Subject: [PATCH] lint Signed-off-by: Vasiliy Tolstov --- .github/workflows/build.yml | 3 +- .github/workflows/pr.yml | 3 +- .golangci.yml | 44 +++++++++++++++++++++++++++ handler.go | 2 +- message.go | 11 ++++--- options.go | 18 +++++------ request.go | 14 +++++---- subscriber.go | 15 ++++++---- tcp.go | 59 +++++++++++++++++++++---------------- 9 files changed, 114 insertions(+), 55 deletions(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eb19b68..465645d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,10 +34,9 @@ jobs: uses: actions/checkout@v2 - name: lint uses: golangci/golangci-lint-action@v2 - continue-on-error: true with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.30 + version: v1.39 # Optional: working directory, useful for monorepos # working-directory: somedir # Optional: golangci-lint command line arguments. diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 545baf2..e339095 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -34,10 +34,9 @@ jobs: uses: actions/checkout@v2 - name: lint uses: golangci/golangci-lint-action@v2 - continue-on-error: true with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.30 + version: v1.39 # Optional: working directory, useful for monorepos # working-directory: somedir # Optional: golangci-lint command line arguments. diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..6ff842d --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,44 @@ +run: + concurrency: 4 + deadline: 5m + issues-exit-code: 1 + tests: true + +linters-settings: + govet: + check-shadowing: true + enable: + - fieldalignment + +linters: + enable: + - govet + - deadcode + - errcheck + - govet + - ineffassign + - staticcheck + - structcheck + - typecheck + - unused + - varcheck + - bodyclose + - gci + - goconst + - gocritic + - gosimple + - gofmt + - gofumpt + - goimports + - golint + - gosec + - makezero + - misspell + - nakedret + - nestif + - nilerr + - noctx + - prealloc + - unconvert + - unparam + disable-all: false diff --git a/handler.go b/handler.go index 597d3e9..77d4da5 100644 --- a/handler.go +++ b/handler.go @@ -13,8 +13,8 @@ type Handler interface { type tcpHandler struct { opts server.HandlerOptions - eps []*register.Endpoint hd interface{} + eps []*register.Endpoint maxMsgSize int } diff --git a/message.go b/message.go index 0925464..3de17a5 100644 --- a/message.go +++ b/message.go @@ -3,15 +3,18 @@ package tcp import ( "github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/metadata" + "github.com/unistack-org/micro/v3/server" ) +var _ server.Message = &tcpMessage{} + type tcpMessage struct { - topic string payload interface{} - contentType string - header metadata.Metadata - body []byte codec codec.Codec + header metadata.Metadata + topic string + contentType string + body []byte } func (r *tcpMessage) Topic() string { diff --git a/options.go b/options.go index 8d5b3e0..b7f6b70 100644 --- a/options.go +++ b/options.go @@ -7,16 +7,16 @@ import ( "github.com/unistack-org/micro/v3/server" ) -var ( - // DefaultMaxMsgSize define maximum message size that server can send - // or receive. Default value is 8K - DefaultMaxMsgSize = 1024 * 8 -) +// DefaultMaxMsgSize define maximum message size that server can send +// or receive. Default value is 8K +var DefaultMaxMsgSize = 1024 * 8 -type maxMsgSizeKey struct{} -type tlsAuth struct{} -type maxConnKey struct{} -type netListener struct{} +type ( + maxMsgSizeKey struct{} + tlsAuth struct{} + maxConnKey struct{} + netListener struct{} +) // // MaxMsgSize set the maximum message in bytes the server can receive and diff --git a/request.go b/request.go index 885ab63..f5fd7fb 100644 --- a/request.go +++ b/request.go @@ -2,16 +2,20 @@ package tcp import ( "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/metadata" + "github.com/unistack-org/micro/v3/server" ) +var _ server.Request = &tcpRequest{} + type tcpRequest struct { - service string + codec codec.Codec + body interface{} + header map[string]string method string endpoint string contentType string - header map[string]string - body interface{} - codec codec.Codec + service string } func (r *tcpRequest) Service() string { @@ -30,7 +34,7 @@ func (r *tcpRequest) ContentType() string { return r.contentType } -func (r *tcpRequest) Header() map[string]string { +func (r *tcpRequest) Header() metadata.Metadata { return r.header } diff --git a/subscriber.go b/subscriber.go index b431bbd..48f7191 100644 --- a/subscriber.go +++ b/subscriber.go @@ -23,9 +23,9 @@ const ( var typeOfError = reflect.TypeOf((*error)(nil)).Elem() type handler struct { - method reflect.Value reqType reflect.Type ctxType reflect.Type + method reflect.Value } type tcpSubscriber struct { @@ -133,7 +133,8 @@ func validateSubscriber(sub server.Subscriber) error { typ := reflect.TypeOf(sub.Subscriber()) var argType reflect.Type - if typ.Kind() == reflect.Func { + switch typ.Kind() { + case reflect.Func: name := "Func" switch typ.NumIn() { case 2: @@ -151,7 +152,7 @@ func validateSubscriber(sub server.Subscriber) error { if returnType := typ.Out(0); returnType != typeOfError { return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String()) } - } else { + default: hdlr := reflect.ValueOf(sub.Subscriber()) name := reflect.Indirect(hdlr).Type().Name() @@ -192,12 +193,14 @@ func (s *tcpServer) createSubHandler(sb *tcpSubscriber, opts server.Options) bro return err } - hdr := make(map[string]string) + hdr := make(map[string]string, len(msg.Header)-1) for k, v := range msg.Header { + if k == "Content-Type" { + continue + } hdr[k] = v } - delete(hdr, "Content-Type") - ctx := metadata.NewContext(context.Background(), hdr) + ctx := metadata.NewIncomingContext(context.Background(), hdr) results := make(chan error, len(sb.handlers)) diff --git a/tcp.go b/tcp.go index 43ad732..2c4f7cb 100644 --- a/tcp.go +++ b/tcp.go @@ -18,17 +18,14 @@ import ( ) type tcpServer struct { + hd server.Handler + rsvc *register.Service + exit chan chan error + subscribers map[*tcpSubscriber][]broker.Subscriber + opts server.Options sync.RWMutex - opts server.Options - hd server.Handler - exit chan chan error - registerOnce sync.Once - subscribers map[*tcpSubscriber][]broker.Subscriber - // used for first registration registered bool init bool - // register service instance - rsvc *register.Service } func (h *tcpServer) newCodec(ct string) (codec.Codec, error) { @@ -89,8 +86,7 @@ func (h *tcpServer) Handle(handler server.Handler) error { func (h *tcpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler { options := server.NewHandlerOptions(opts...) - var eps []*register.Endpoint - + eps := make([]*register.Endpoint, 0, len(options.Metadata)) for name, metadata := range options.Metadata { eps = append(eps, ®ister.Endpoint{ Name: name, @@ -157,12 +153,14 @@ func (h *tcpServer) Register() error { if err != nil { return err } + service.Nodes[0].Metadata["protocol"] = "tcp" - service.Nodes[0].Metadata["transport"] = "tcp" + service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"] service.Endpoints = eps h.Lock() - var subscriberList []*tcpSubscriber + + subscriberList := make([]*tcpSubscriber, 0, len(h.subscribers)) for e := range h.subscribers { // Only advertise non internal subscribers subscriberList = append(subscriberList, e) @@ -312,10 +310,13 @@ func (h *tcpServer) Start() error { if l := h.getListener(); l != nil { ts = l - } else { + } + + // nolint: nestif + if ts == nil { // check the tls config for secure connect - if tc := config.TLSConfig; tc != nil { - ts, err = tls.Listen("tcp", config.Address, tc) + if config.TLSConfig != nil { + ts, err = tls.Listen("tcp", config.Address, config.TLSConfig) // otherwise just plain tcp listener } else { ts, err = net.Listen("tcp", config.Address) @@ -375,6 +376,7 @@ func (h *tcpServer) Start() error { registered := h.registered h.RUnlock() rerr := h.opts.RegisterCheck(h.opts.Context) + // nolint: nestif if rerr != nil && registered { if config.Logger.V(logger.ErrorLevel) { config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr) @@ -405,9 +407,13 @@ func (h *tcpServer) Start() error { ch <- ts.Close() // deregister - h.Deregister() + if cerr := h.Deregister(); cerr != nil { + config.Logger.Errorf(config.Context, "Register deregister error: %v", cerr) + } - config.Broker.Disconnect(config.Context) + if cerr := config.Broker.Disconnect(config.Context); cerr != nil { + config.Logger.Errorf(config.Context, "Broker disconnect error: %v", cerr) + } }() return nil @@ -419,24 +425,25 @@ func (h *tcpServer) Stop() error { return <-ch } -func (s *tcpServer) String() string { +func (h *tcpServer) String() string { return "tcp" } -func (s *tcpServer) Name() string { - return s.opts.Name +func (h *tcpServer) Name() string { + return h.opts.Name } -func (s *tcpServer) serve(ln net.Listener, h Handler) { +func (h *tcpServer) serve(ln net.Listener, hd Handler) { var tempDelay time.Duration // how long to sleep on accept failure - s.RLock() - config := s.opts - s.RUnlock() + h.RLock() + config := h.opts + h.RUnlock() for { c, err := ln.Accept() + // nolint: nestif if err != nil { select { - case <-s.exit: + case <-h.exit: return default: } @@ -465,7 +472,7 @@ func (s *tcpServer) serve(ln net.Listener, h Handler) { config.Logger.Errorf(config.Context, "tcp: accept err: %v", err) return } - go h.Serve(c) + go hd.Serve(c) } } -- 2.45.2