parent
1d0a53666a
commit
f6c43cbf9e
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@ -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.
|
||||
|
3
.github/workflows/pr.yml
vendored
3
.github/workflows/pr.yml
vendored
@ -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.
|
||||
|
44
.golangci.yml
Normal file
44
.golangci.yml
Normal file
@ -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
|
@ -13,8 +13,8 @@ type Handler interface {
|
||||
|
||||
type tcpHandler struct {
|
||||
opts server.HandlerOptions
|
||||
eps []*register.Endpoint
|
||||
hd interface{}
|
||||
eps []*register.Endpoint
|
||||
maxMsgSize int
|
||||
}
|
||||
|
||||
|
11
message.go
11
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 {
|
||||
|
18
options.go
18
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
|
||||
|
14
request.go
14
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
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
|
||||
|
59
tcp.go
59
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user