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
|
uses: actions/checkout@v2
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v2
|
||||||
continue-on-error: true
|
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# 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
|
# Optional: working directory, useful for monorepos
|
||||||
# working-directory: somedir
|
# working-directory: somedir
|
||||||
# Optional: golangci-lint command line arguments.
|
# 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
|
uses: actions/checkout@v2
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v2
|
||||||
continue-on-error: true
|
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# 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
|
# Optional: working directory, useful for monorepos
|
||||||
# working-directory: somedir
|
# working-directory: somedir
|
||||||
# Optional: golangci-lint command line arguments.
|
# 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 {
|
type tcpHandler struct {
|
||||||
opts server.HandlerOptions
|
opts server.HandlerOptions
|
||||||
eps []*register.Endpoint
|
|
||||||
hd interface{}
|
hd interface{}
|
||||||
|
eps []*register.Endpoint
|
||||||
maxMsgSize int
|
maxMsgSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
message.go
11
message.go
@ -3,15 +3,18 @@ package tcp
|
|||||||
import (
|
import (
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
|
"github.com/unistack-org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ server.Message = &tcpMessage{}
|
||||||
|
|
||||||
type tcpMessage struct {
|
type tcpMessage struct {
|
||||||
topic string
|
|
||||||
payload interface{}
|
payload interface{}
|
||||||
contentType string
|
|
||||||
header metadata.Metadata
|
|
||||||
body []byte
|
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
|
header metadata.Metadata
|
||||||
|
topic string
|
||||||
|
contentType string
|
||||||
|
body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tcpMessage) Topic() string {
|
func (r *tcpMessage) Topic() string {
|
||||||
|
18
options.go
18
options.go
@ -7,16 +7,16 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// DefaultMaxMsgSize define maximum message size that server can send
|
||||||
// DefaultMaxMsgSize define maximum message size that server can send
|
// or receive. Default value is 8K
|
||||||
// or receive. Default value is 8K
|
var DefaultMaxMsgSize = 1024 * 8
|
||||||
DefaultMaxMsgSize = 1024 * 8
|
|
||||||
)
|
|
||||||
|
|
||||||
type maxMsgSizeKey struct{}
|
type (
|
||||||
type tlsAuth struct{}
|
maxMsgSizeKey struct{}
|
||||||
type maxConnKey struct{}
|
tlsAuth struct{}
|
||||||
type netListener struct{}
|
maxConnKey struct{}
|
||||||
|
netListener struct{}
|
||||||
|
)
|
||||||
|
|
||||||
//
|
//
|
||||||
// MaxMsgSize set the maximum message in bytes the server can receive and
|
// 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 (
|
import (
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"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 {
|
type tcpRequest struct {
|
||||||
service string
|
codec codec.Codec
|
||||||
|
body interface{}
|
||||||
|
header map[string]string
|
||||||
method string
|
method string
|
||||||
endpoint string
|
endpoint string
|
||||||
contentType string
|
contentType string
|
||||||
header map[string]string
|
service string
|
||||||
body interface{}
|
|
||||||
codec codec.Codec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tcpRequest) Service() string {
|
func (r *tcpRequest) Service() string {
|
||||||
@ -30,7 +34,7 @@ func (r *tcpRequest) ContentType() string {
|
|||||||
return r.contentType
|
return r.contentType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tcpRequest) Header() map[string]string {
|
func (r *tcpRequest) Header() metadata.Metadata {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,9 +23,9 @@ const (
|
|||||||
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
method reflect.Value
|
|
||||||
reqType reflect.Type
|
reqType reflect.Type
|
||||||
ctxType reflect.Type
|
ctxType reflect.Type
|
||||||
|
method reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
type tcpSubscriber struct {
|
type tcpSubscriber struct {
|
||||||
@ -133,7 +133,8 @@ func validateSubscriber(sub server.Subscriber) error {
|
|||||||
typ := reflect.TypeOf(sub.Subscriber())
|
typ := reflect.TypeOf(sub.Subscriber())
|
||||||
var argType reflect.Type
|
var argType reflect.Type
|
||||||
|
|
||||||
if typ.Kind() == reflect.Func {
|
switch typ.Kind() {
|
||||||
|
case reflect.Func:
|
||||||
name := "Func"
|
name := "Func"
|
||||||
switch typ.NumIn() {
|
switch typ.NumIn() {
|
||||||
case 2:
|
case 2:
|
||||||
@ -151,7 +152,7 @@ func validateSubscriber(sub server.Subscriber) error {
|
|||||||
if returnType := typ.Out(0); returnType != typeOfError {
|
if returnType := typ.Out(0); returnType != typeOfError {
|
||||||
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
||||||
}
|
}
|
||||||
} else {
|
default:
|
||||||
hdlr := reflect.ValueOf(sub.Subscriber())
|
hdlr := reflect.ValueOf(sub.Subscriber())
|
||||||
name := reflect.Indirect(hdlr).Type().Name()
|
name := reflect.Indirect(hdlr).Type().Name()
|
||||||
|
|
||||||
@ -192,12 +193,14 @@ func (s *tcpServer) createSubHandler(sb *tcpSubscriber, opts server.Options) bro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
hdr := make(map[string]string)
|
hdr := make(map[string]string, len(msg.Header)-1)
|
||||||
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.NewIncomingContext(context.Background(), hdr)
|
||||||
ctx := metadata.NewContext(context.Background(), hdr)
|
|
||||||
|
|
||||||
results := make(chan error, len(sb.handlers))
|
results := make(chan error, len(sb.handlers))
|
||||||
|
|
||||||
|
59
tcp.go
59
tcp.go
@ -18,17 +18,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type tcpServer struct {
|
type tcpServer struct {
|
||||||
|
hd server.Handler
|
||||||
|
rsvc *register.Service
|
||||||
|
exit chan chan error
|
||||||
|
subscribers map[*tcpSubscriber][]broker.Subscriber
|
||||||
|
opts server.Options
|
||||||
sync.RWMutex
|
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
|
registered bool
|
||||||
init bool
|
init bool
|
||||||
// register service instance
|
|
||||||
rsvc *register.Service
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *tcpServer) newCodec(ct string) (codec.Codec, error) {
|
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 {
|
func (h *tcpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
||||||
options := server.NewHandlerOptions(opts...)
|
options := server.NewHandlerOptions(opts...)
|
||||||
|
|
||||||
var eps []*register.Endpoint
|
eps := make([]*register.Endpoint, 0, len(options.Metadata))
|
||||||
|
|
||||||
for name, metadata := range options.Metadata {
|
for name, metadata := range options.Metadata {
|
||||||
eps = append(eps, ®ister.Endpoint{
|
eps = append(eps, ®ister.Endpoint{
|
||||||
Name: name,
|
Name: name,
|
||||||
@ -157,12 +153,14 @@ func (h *tcpServer) Register() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
service.Nodes[0].Metadata["protocol"] = "tcp"
|
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
|
service.Endpoints = eps
|
||||||
|
|
||||||
h.Lock()
|
h.Lock()
|
||||||
var subscriberList []*tcpSubscriber
|
|
||||||
|
subscriberList := make([]*tcpSubscriber, 0, len(h.subscribers))
|
||||||
for e := range h.subscribers {
|
for e := range h.subscribers {
|
||||||
// Only advertise non internal subscribers
|
// Only advertise non internal subscribers
|
||||||
subscriberList = append(subscriberList, e)
|
subscriberList = append(subscriberList, e)
|
||||||
@ -312,10 +310,13 @@ func (h *tcpServer) Start() error {
|
|||||||
|
|
||||||
if l := h.getListener(); l != nil {
|
if l := h.getListener(); l != nil {
|
||||||
ts = l
|
ts = l
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
// nolint: nestif
|
||||||
|
if ts == nil {
|
||||||
// check the tls config for secure connect
|
// check the tls config for secure connect
|
||||||
if tc := config.TLSConfig; tc != nil {
|
if config.TLSConfig != nil {
|
||||||
ts, err = tls.Listen("tcp", config.Address, tc)
|
ts, err = tls.Listen("tcp", config.Address, config.TLSConfig)
|
||||||
// otherwise just plain tcp listener
|
// otherwise just plain tcp listener
|
||||||
} else {
|
} else {
|
||||||
ts, err = net.Listen("tcp", config.Address)
|
ts, err = net.Listen("tcp", config.Address)
|
||||||
@ -375,6 +376,7 @@ func (h *tcpServer) Start() error {
|
|||||||
registered := h.registered
|
registered := h.registered
|
||||||
h.RUnlock()
|
h.RUnlock()
|
||||||
rerr := h.opts.RegisterCheck(h.opts.Context)
|
rerr := h.opts.RegisterCheck(h.opts.Context)
|
||||||
|
// nolint: nestif
|
||||||
if rerr != nil && registered {
|
if rerr != nil && registered {
|
||||||
if config.Logger.V(logger.ErrorLevel) {
|
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)
|
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()
|
ch <- ts.Close()
|
||||||
|
|
||||||
// deregister
|
// 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
|
return nil
|
||||||
@ -419,24 +425,25 @@ func (h *tcpServer) Stop() error {
|
|||||||
return <-ch
|
return <-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *tcpServer) String() string {
|
func (h *tcpServer) String() string {
|
||||||
return "tcp"
|
return "tcp"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *tcpServer) Name() string {
|
func (h *tcpServer) Name() string {
|
||||||
return s.opts.Name
|
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
|
var tempDelay time.Duration // how long to sleep on accept failure
|
||||||
s.RLock()
|
h.RLock()
|
||||||
config := s.opts
|
config := h.opts
|
||||||
s.RUnlock()
|
h.RUnlock()
|
||||||
for {
|
for {
|
||||||
c, err := ln.Accept()
|
c, err := ln.Accept()
|
||||||
|
// nolint: nestif
|
||||||
if err != nil {
|
if err != nil {
|
||||||
select {
|
select {
|
||||||
case <-s.exit:
|
case <-h.exit:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
@ -465,7 +472,7 @@ func (s *tcpServer) serve(ln net.Listener, h Handler) {
|
|||||||
config.Logger.Errorf(config.Context, "tcp: accept err: %v", err)
|
config.Logger.Errorf(config.Context, "tcp: accept err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go h.Serve(c)
|
go hd.Serve(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user