From 4bf874bfe7e2d89d5baaa626c0d676c57ae1a5ad Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 26 Apr 2021 00:35:33 +0300 Subject: [PATCH] lint Signed-off-by: Vasiliy Tolstov --- .github/stale.sh | 13 ----------- .github/workflows/build.yml | 3 +-- .github/workflows/pr.yml | 3 +-- .golangci.yml | 44 +++++++++++++++++++++++++++++++++++++ handler.go | 40 ++++++++++++--------------------- http.go | 42 +++++++++++++++++++++++------------ options.go | 32 ++++++++++++++++----------- request.go | 9 ++++++-- server.go | 7 +++--- subscriber.go | 6 +---- 10 files changed, 118 insertions(+), 81 deletions(-) delete mode 100755 .github/stale.sh create mode 100644 .golangci.yml diff --git a/.github/stale.sh b/.github/stale.sh deleted file mode 100755 index 8a345c4..0000000 --- a/.github/stale.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -ex - -export PATH=$PATH:$(pwd)/bin -export GO111MODULE=on -export GOBIN=$(pwd)/bin - -#go get github.com/rvflash/goup@v0.4.1 - -#goup -v ./... -#go get github.com/psampaz/go-mod-outdated@v0.6.0 -go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true - -#go list -u -m -json all | go-mod-outdated -update 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 6b7b6b7..fec9e3e 100644 --- a/handler.go +++ b/handler.go @@ -9,7 +9,6 @@ import ( "strings" "sync" - "github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/errors" "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/metadata" @@ -22,7 +21,9 @@ import ( var ( DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) { w.WriteHeader(status) - w.Write([]byte(err.Error())) + if _, cerr := w.Write([]byte(err.Error())); cerr != nil { + logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr) + } } DefaultContentType = "application/json" ) @@ -44,20 +45,6 @@ type httpHandler struct { sync.RWMutex } -func (h *httpHandler) newCodec(ct string) (codec.Codec, error) { - h.RLock() - defer h.RUnlock() - - if idx := strings.IndexRune(ct, ';'); idx >= 0 { - ct = ct[:idx] - } - - if cf, ok := h.sopts.Codecs[ct]; ok { - return cf, nil - } - return nil, codec.ErrUnknownContentType -} - func (h *httpHandler) Name() string { return h.name } @@ -75,7 +62,6 @@ func (h *httpHandler) Options() server.HandlerOptions { } func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - for exp, ph := range h.pathHandlers { if exp.MatchString(r.URL.String()) { ph(w, r) @@ -95,7 +81,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - ctx := metadata.NewContext(r.Context(), nil) + ctx := metadata.NewIncomingContext(r.Context(), nil) defer r.Body.Close() @@ -132,8 +118,8 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, hpat := range h.handlers { handlertmp := hpat.(*httpHandler) for _, hldrtmp := range handlertmp.handlers[r.Method] { - mp, err := hldrtmp.pat.Match(components, verb) - if err == nil { + mp, merr := hldrtmp.pat.Match(components, verb) + if merr == nil { match = true for k, v := range mp { matches[k] = v @@ -161,9 +147,9 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { // get fields from url values if len(r.URL.RawQuery) > 0 { - umd, err := rflutil.URLMap(r.URL.RawQuery) - if err != nil { - h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest) + umd, cerr := rflutil.URLMap(r.URL.RawQuery) + if cerr != nil { + h.errorHandler(ctx, handler, w, r, cerr, http.StatusBadRequest) return } for k, v := range umd { @@ -190,7 +176,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { replyv = reflect.New(hldr.mtype.ReplyType.Elem()) function := hldr.mtype.method.Func - //function := hldr.rcvr + // function := hldr.rcvr var returnValues []reflect.Value if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF { @@ -281,8 +267,10 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if scode != 0 { w.WriteHeader(scode) } else { - //handler.sopts.Logger.Warn(handler.sopts.Context, "response code not set in handler via SetRspCode(ctx, http.StatusXXX)") + // handler.sopts.Logger.Warn(handler.sopts.Context, "response code not set in handler via SetRspCode(ctx, http.StatusXXX)") w.WriteHeader(200) } - w.Write(b) + if _, cerr := w.Write(b); cerr != nil { + logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr) + } } diff --git a/http.go b/http.go index 0de3c8d..febc2f6 100644 --- a/http.go +++ b/http.go @@ -126,19 +126,23 @@ func (h *httpServer) Init(opts ...server.Option) error { func (h *httpServer) Handle(handler server.Handler) error { h.Lock() - if hdlr, ok := handler.(*httpHandler); ok { - if _, ok := hdlr.hd.(http.Handler); ok { - h.hd = handler - } else { - if h.handlers == nil { - h.handlers = make(map[string]server.Handler) - } - h.handlers[handler.Name()] = handler - } - } else { + defer h.Unlock() + hdlr, ok := handler.(*httpHandler) + if !ok { h.hd = handler + return nil } - h.Unlock() + + if _, ok := hdlr.hd.(http.Handler); ok { + h.hd = handler + return nil + } + + if h.handlers == nil { + h.handlers = make(map[string]server.Handler) + } + h.handlers[handler.Name()] = handler + return nil } @@ -279,7 +283,7 @@ func (h *httpServer) Register() error { service.Endpoints = eps h.Lock() - var subscriberList []*httpSubscriber + subscriberList := make([]*httpSubscriber, 0, len(h.subscribers)) for e := range h.subscribers { // Only advertise non internal subscribers subscriberList = append(subscriberList, e) @@ -430,6 +434,7 @@ func (h *httpServer) Start() error { var handler http.Handler var srvFunc func(net.Listener) error + // nolint: nestif if h.opts.Context != nil { if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil { if hs.Handler == nil && h.hd != nil { @@ -485,9 +490,17 @@ func (h *httpServer) Start() error { } if srvFunc != nil { - go srvFunc(ts) + go func() { + if cerr := srvFunc(ts); cerr != nil { + h.opts.Logger.Error(h.opts.Context, cerr) + } + }() } else { - go http.Serve(ts, fn) + go func() { + if cerr := http.Serve(ts, fn); cerr != nil { + h.opts.Logger.Error(h.opts.Context, cerr) + } + }() } go func() { @@ -511,6 +524,7 @@ func (h *httpServer) Start() error { registered := h.registered h.RUnlock() rerr := config.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) diff --git a/options.go b/options.go index 66fa6fc..4173e5a 100644 --- a/options.go +++ b/options.go @@ -8,11 +8,6 @@ import ( "github.com/unistack-org/micro/v3/server" ) -type rspCodeKey struct{} -type rspCodeVal struct { - code int -} - // SetError pass error to caller func SetError(err interface{}) error { return &Error{err: err} @@ -28,6 +23,13 @@ func (err *Error) Error() string { return fmt.Sprintf("%v", err.err) } +type ( + rspCodeKey struct{} + rspCodeVal struct { + code int + } +) + // SetRspCode saves response code in context, must be used by handler to specify http code func SetRspCode(ctx context.Context, code int) { if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok { @@ -65,10 +67,12 @@ func ErrorHandler(fn func(ctx context.Context, s server.Handler, w http.Response return server.SetOption(errorHandlerKey{}, fn) } -type pathHandlerKey struct{} -type pathHandlerVal struct { - h map[string]http.HandlerFunc -} +type ( + pathHandlerKey struct{} + pathHandlerVal struct { + h map[string]http.HandlerFunc + } +) // PathHandler specifies http handler for path regexp func PathHandler(path string, h http.HandlerFunc) server.Option { @@ -85,10 +89,12 @@ func PathHandler(path string, h http.HandlerFunc) server.Option { } } -type contentTypeHandlerKey struct{} -type contentTypeHandlerVal struct { - h map[string]http.HandlerFunc -} +type ( + contentTypeHandlerKey struct{} + contentTypeHandlerVal struct { + h map[string]http.HandlerFunc + } +) // ContentTypeHandler specifies http handler for Content-Type func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option { diff --git a/request.go b/request.go index 036a4cb..6ee7c05 100644 --- a/request.go +++ b/request.go @@ -5,6 +5,12 @@ 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 = &rpcRequest{} + _ server.Message = &rpcMessage{} ) type rpcRequest struct { @@ -16,7 +22,6 @@ type rpcRequest struct { endpoint string contentType string service string - target string body []byte stream bool } @@ -43,7 +48,7 @@ func (r *rpcRequest) Method() string { } func (r *rpcRequest) Endpoint() string { - return r.method + return r.endpoint } func (r *rpcRequest) Codec() codec.Codec { diff --git a/server.go b/server.go index 166dad9..9f9bfce 100644 --- a/server.go +++ b/server.go @@ -62,15 +62,14 @@ func prepareEndpoint(method reflect.Method) (*methodType, error) { return nil, fmt.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn()) } - if stream { + switch stream { + case true: // check stream type streamType := reflect.TypeOf((*server.Stream)(nil)).Elem() if !argType.Implements(streamType) { return nil, fmt.Errorf("%v argument does not implement Streamer interface: %v", mname, argType) } - } else { - // if not stream check the replyType - + default: // First arg need not be a pointer. if !isExportedOrBuiltinType(argType) { return nil, fmt.Errorf("%v argument type not exported: %v", mname, argType) diff --git a/subscriber.go b/subscriber.go index 0e89055..7a5c0b3 100644 --- a/subscriber.go +++ b/subscriber.go @@ -14,10 +14,6 @@ import ( "github.com/unistack-org/micro/v3/server" ) -const ( - subSig = "func(context.Context, interface{}) error" -) - var typeOfError = reflect.TypeOf((*error)(nil)).Elem() type handler struct { @@ -116,7 +112,7 @@ func (s *httpServer) createSubHandler(sb *httpSubscriber, opts server.Options) b hdr := metadata.Copy(msg.Header) delete(hdr, "Content-Type") - ctx := metadata.NewContext(context.Background(), hdr) + ctx := metadata.NewIncomingContext(context.Background(), hdr) results := make(chan error, len(sb.handlers))