2020-09-03 12:21:00 +03:00
|
|
|
// Package tcp implements a go-micro.Server
|
2021-10-27 01:04:58 +03:00
|
|
|
package tcp // import "go.unistack.org/micro-server-tcp/v3"
|
2020-09-03 12:21:00 +03:00
|
|
|
|
|
|
|
import (
|
2024-03-13 15:36:34 +03:00
|
|
|
"context"
|
2020-09-03 12:21:00 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-27 01:04:58 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2024-03-13 15:36:34 +03:00
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
"golang.org/x/net/netutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tcpServer struct {
|
2021-04-26 01:10:37 +03:00
|
|
|
hd server.Handler
|
|
|
|
rsvc *register.Service
|
|
|
|
exit chan chan error
|
|
|
|
subscribers map[*tcpSubscriber][]broker.Subscriber
|
|
|
|
opts server.Options
|
2020-09-03 12:21:00 +03:00
|
|
|
sync.RWMutex
|
|
|
|
registered bool
|
2021-03-24 15:25:06 +03:00
|
|
|
init bool
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 23:56:38 +03:00
|
|
|
func (h *tcpServer) newCodec(ct string) (codec.Codec, error) {
|
|
|
|
if cf, ok := h.opts.Codecs[ct]; ok {
|
2020-09-03 12:21:00 +03:00
|
|
|
return cf, nil
|
|
|
|
}
|
2020-11-25 23:56:38 +03:00
|
|
|
return nil, codec.ErrUnknownContentType
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Options() server.Options {
|
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Init(opts ...server.Option) error {
|
2021-03-24 15:25:06 +03:00
|
|
|
if len(opts) == 0 && h.init {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
h.Lock()
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&h.opts)
|
|
|
|
}
|
|
|
|
h.Unlock()
|
2021-03-24 15:25:06 +03:00
|
|
|
|
|
|
|
if err := h.opts.Register.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Broker.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Transport.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Handle(handler server.Handler) error {
|
|
|
|
h.Lock()
|
|
|
|
h.hd = handler
|
|
|
|
h.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
2020-11-18 13:02:53 +03:00
|
|
|
options := server.NewHandlerOptions(opts...)
|
2020-09-03 12:21:00 +03:00
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
eps := make([]*register.Endpoint, 0, len(options.Metadata))
|
2021-04-20 14:27:30 +03:00
|
|
|
for name, metadata := range options.Metadata {
|
|
|
|
eps = append(eps, ®ister.Endpoint{
|
|
|
|
Name: name,
|
|
|
|
Metadata: metadata,
|
|
|
|
})
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
th := &tcpHandler{
|
|
|
|
eps: eps,
|
|
|
|
hd: handler,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
|
|
|
|
if size, ok := h.opts.Context.Value(maxMsgSizeKey{}).(int); ok && size > 0 {
|
|
|
|
th.maxMsgSize = size
|
|
|
|
}
|
|
|
|
|
|
|
|
return th
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
|
|
|
return newSubscriber(topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Subscribe(sb server.Subscriber) error {
|
|
|
|
sub, ok := sb.(*tcpSubscriber)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid subscriber: expected *tcpSubscriber")
|
|
|
|
}
|
|
|
|
if len(sub.handlers) == 0 {
|
|
|
|
return fmt.Errorf("invalid subscriber: no handler functions")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSubscriber(sb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
_, ok = h.subscribers[sub]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("subscriber %v already exists", h)
|
|
|
|
}
|
|
|
|
h.subscribers[sub] = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Register() error {
|
|
|
|
h.Lock()
|
2020-11-05 02:12:36 +03:00
|
|
|
config := h.opts
|
|
|
|
rsvc := h.rsvc
|
2020-09-03 12:21:00 +03:00
|
|
|
eps := h.hd.Endpoints()
|
|
|
|
h.Unlock()
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
// if service already filled, reuse it and return early
|
|
|
|
if rsvc != nil {
|
|
|
|
if err := server.DefaultRegisterFunc(rsvc, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:16:03 +03:00
|
|
|
service, err := server.NewRegisterService(h)
|
2020-10-09 17:46:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-26 01:10:37 +03:00
|
|
|
|
2020-10-16 16:48:52 +03:00
|
|
|
service.Nodes[0].Metadata["protocol"] = "tcp"
|
2021-04-26 01:10:37 +03:00
|
|
|
service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"]
|
2020-09-03 12:21:00 +03:00
|
|
|
service.Endpoints = eps
|
|
|
|
|
|
|
|
h.Lock()
|
2021-04-26 01:10:37 +03:00
|
|
|
|
|
|
|
subscriberList := make([]*tcpSubscriber, 0, len(h.subscribers))
|
2020-09-03 12:21:00 +03:00
|
|
|
for e := range h.subscribers {
|
|
|
|
// Only advertise non internal subscribers
|
2021-04-20 14:27:30 +03:00
|
|
|
subscriberList = append(subscriberList, e)
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
sort.Slice(subscriberList, func(i, j int) bool {
|
|
|
|
return subscriberList[i].topic > subscriberList[j].topic
|
|
|
|
})
|
|
|
|
for _, e := range subscriberList {
|
|
|
|
service.Endpoints = append(service.Endpoints, e.Endpoints()...)
|
|
|
|
}
|
|
|
|
h.Unlock()
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
h.RLock()
|
|
|
|
registered := h.registered
|
|
|
|
h.RUnlock()
|
2020-09-03 12:21:00 +03:00
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
if !registered {
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID)
|
2020-11-05 02:12:36 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
// register the service
|
|
|
|
if err := server.DefaultRegisterFunc(service, config); err != nil {
|
2020-09-03 12:21:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
// already registered? don't need to register subscribers
|
|
|
|
if registered {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.registered {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-16 16:48:52 +03:00
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
for sb := range h.subscribers {
|
2020-11-05 02:12:36 +03:00
|
|
|
handler := h.createSubHandler(sb, config)
|
|
|
|
var opts []broker.SubscribeOption
|
2020-09-03 12:21:00 +03:00
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
2020-11-05 02:12:36 +03:00
|
|
|
opts = append(opts, broker.SubscribeGroup(queue))
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
|
|
|
|
subCtx := config.Context
|
2020-10-16 16:48:52 +03:00
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
opts = append(opts, broker.SubscribeContext(subCtx))
|
|
|
|
opts = append(opts, broker.SubscribeAutoAck(sb.Options().AutoAck))
|
|
|
|
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-01-16 00:42:40 +03:00
|
|
|
config.Logger.Infof(config.Context, "Subscribing to topic: %s", sb.Topic())
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
2020-09-03 12:21:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
|
|
|
|
h.registered = true
|
|
|
|
h.rsvc = service
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Deregister() error {
|
|
|
|
h.Lock()
|
2020-11-05 02:12:36 +03:00
|
|
|
config := h.opts
|
2020-09-03 12:21:00 +03:00
|
|
|
h.Unlock()
|
|
|
|
|
2021-01-29 15:16:03 +03:00
|
|
|
service, err := server.NewRegisterService(h)
|
2020-10-09 17:46:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].ID)
|
2020-11-05 02:12:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
2020-09-03 12:21:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
if !h.registered {
|
|
|
|
h.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h.registered = false
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
wg := sync.WaitGroup{}
|
2020-10-16 16:48:52 +03:00
|
|
|
subCtx := h.opts.Context
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
for sb, subs := range h.subscribers {
|
2020-10-16 16:48:52 +03:00
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
for _, sub := range subs {
|
2020-11-05 02:12:36 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(s broker.Subscriber) {
|
|
|
|
defer wg.Done()
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-01-16 00:42:40 +03:00
|
|
|
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", s.Topic())
|
2020-11-05 02:12:36 +03:00
|
|
|
}
|
|
|
|
if err := s.Unsubscribe(subCtx); err != nil {
|
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:42:40 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Unsubscribing from topic: %s err: %v", s.Topic(), err)
|
2020-11-05 02:12:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(sub)
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
h.subscribers[sb] = nil
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
wg.Wait()
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
h.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) getListener() net.Listener {
|
|
|
|
if h.opts.Context == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l, ok := h.opts.Context.Value(netListener{}).(net.Listener)
|
|
|
|
if !ok || l == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Start() error {
|
2020-11-05 02:12:36 +03:00
|
|
|
h.RLock()
|
|
|
|
config := h.opts
|
2020-09-03 12:21:00 +03:00
|
|
|
hd := h.hd.Handler()
|
2020-11-05 02:12:36 +03:00
|
|
|
h.RUnlock()
|
2020-09-03 12:21:00 +03:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var ts net.Listener
|
|
|
|
|
|
|
|
if l := h.getListener(); l != nil {
|
|
|
|
ts = l
|
2021-04-26 01:10:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: nestif
|
|
|
|
if ts == nil {
|
2020-09-03 12:21:00 +03:00
|
|
|
// check the tls config for secure connect
|
2021-04-26 01:10:37 +03:00
|
|
|
if config.TLSConfig != nil {
|
|
|
|
ts, err = tls.Listen("tcp", config.Address, config.TLSConfig)
|
2020-09-03 12:21:00 +03:00
|
|
|
// otherwise just plain tcp listener
|
|
|
|
} else {
|
2020-11-05 02:12:36 +03:00
|
|
|
ts, err = net.Listen("tcp", config.Address)
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Context != nil {
|
|
|
|
if c, ok := config.Context.Value(maxConnKey{}).(int); ok && c > 0 {
|
2020-09-03 12:21:00 +03:00
|
|
|
ts = netutil.LimitListener(ts, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:42:40 +03:00
|
|
|
config.Logger.Infof(config.Context, "Listening on %s", ts.Addr().String())
|
2020-11-05 02:12:36 +03:00
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.opts.Address = ts.Addr().String()
|
|
|
|
h.Unlock()
|
|
|
|
|
2020-11-05 02:12:36 +03:00
|
|
|
if err = config.Broker.Connect(config.Context); err != nil {
|
2020-09-03 12:21:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// register
|
|
|
|
if err = h.Register(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
handle, ok := hd.(Handler)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid handler %T", hd)
|
|
|
|
}
|
|
|
|
go h.serve(ts, handle)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
t := new(time.Ticker)
|
|
|
|
|
|
|
|
// only process if it exists
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.RegisterInterval > time.Duration(0) {
|
2020-09-03 12:21:00 +03:00
|
|
|
// new ticker
|
2020-11-05 02:12:36 +03:00
|
|
|
t = time.NewTicker(config.RegisterInterval)
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return error chan
|
|
|
|
var ch chan error
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// register self on interval
|
|
|
|
case <-t.C:
|
2020-09-10 09:52:09 +03:00
|
|
|
h.RLock()
|
|
|
|
registered := h.registered
|
|
|
|
h.RUnlock()
|
|
|
|
rerr := h.opts.RegisterCheck(h.opts.Context)
|
2021-04-26 01:10:37 +03:00
|
|
|
// nolint: nestif
|
2020-09-10 09:52:09 +03:00
|
|
|
if rerr != nil && registered {
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr)
|
2020-09-10 09:52:09 +03:00
|
|
|
}
|
|
|
|
// deregister self in case of error
|
|
|
|
if err := h.Deregister(); err != nil {
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.ID, err)
|
2020-09-10 09:52:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if rerr != nil && !registered {
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, rerr)
|
2020-09-10 09:52:09 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
if err := h.Register(); err != nil {
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-04-27 19:12:53 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.ID, err)
|
2020-09-10 09:52:09 +03:00
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
2020-09-10 09:52:09 +03:00
|
|
|
// wait for exit
|
2020-09-03 12:21:00 +03:00
|
|
|
case ch = <-h.exit:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:36:34 +03:00
|
|
|
h.gracefulStop()
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
ch <- ts.Close()
|
|
|
|
|
|
|
|
// deregister
|
2021-04-26 01:10:37 +03:00
|
|
|
if cerr := h.Deregister(); cerr != nil {
|
|
|
|
config.Logger.Errorf(config.Context, "Register deregister error: %v", cerr)
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
if cerr := config.Broker.Disconnect(config.Context); cerr != nil {
|
|
|
|
config.Logger.Errorf(config.Context, "Broker disconnect error: %v", cerr)
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tcpServer) Stop() error {
|
|
|
|
ch := make(chan error)
|
|
|
|
h.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:36:34 +03:00
|
|
|
func (h *tcpServer) gracefulStop() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), h.opts.GracefulTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
h.opts.Wait.WaitContext(ctx)
|
|
|
|
}
|
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
func (h *tcpServer) String() string {
|
2020-09-03 12:21:00 +03:00
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
func (h *tcpServer) Name() string {
|
|
|
|
return h.opts.Name
|
2021-01-29 15:16:03 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
func (h *tcpServer) serve(ln net.Listener, hd Handler) {
|
2020-09-07 17:04:55 +03:00
|
|
|
var tempDelay time.Duration // how long to sleep on accept failure
|
2021-04-26 01:10:37 +03:00
|
|
|
h.RLock()
|
|
|
|
config := h.opts
|
|
|
|
h.RUnlock()
|
2020-09-03 12:21:00 +03:00
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
2021-04-26 01:10:37 +03:00
|
|
|
// nolint: nestif
|
2020-09-07 17:04:55 +03:00
|
|
|
if err != nil {
|
|
|
|
select {
|
2021-04-26 01:10:37 +03:00
|
|
|
case <-h.exit:
|
2020-09-07 17:04:55 +03:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if ne, ok := err.(net.Error); ok && ne.Temporary() {
|
|
|
|
if tempDelay == 0 {
|
|
|
|
tempDelay = 5 * time.Millisecond
|
|
|
|
} else {
|
|
|
|
tempDelay *= 2
|
|
|
|
}
|
|
|
|
if max := 1 * time.Second; tempDelay > max {
|
|
|
|
tempDelay = max
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:42:40 +03:00
|
|
|
config.Logger.Errorf(config.Context, "tcp: Accept error: %v; retrying in %v", err, tempDelay)
|
2020-09-07 17:04:55 +03:00
|
|
|
}
|
|
|
|
time.Sleep(tempDelay)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-05 02:12:36 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-03-30 15:40:24 +03:00
|
|
|
config.Logger.Errorf(config.Context, "tcp: Accept error: %v", err)
|
2020-09-07 17:04:55 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:21:00 +03:00
|
|
|
if err != nil {
|
2021-03-30 15:40:24 +03:00
|
|
|
config.Logger.Errorf(config.Context, "tcp: accept err: %v", err)
|
2020-09-07 17:04:55 +03:00
|
|
|
return
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
2024-03-13 15:36:34 +03:00
|
|
|
|
|
|
|
h.opts.Wait.Add(1)
|
|
|
|
go func() {
|
|
|
|
hd.Serve(c)
|
|
|
|
h.opts.Wait.Done()
|
|
|
|
}()
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:16:03 +03:00
|
|
|
func NewServer(opts ...server.Option) server.Server {
|
2020-09-03 12:21:00 +03:00
|
|
|
return &tcpServer{
|
2020-09-10 00:27:29 +03:00
|
|
|
opts: server.NewOptions(opts...),
|
2020-09-03 12:21:00 +03:00
|
|
|
exit: make(chan chan error),
|
|
|
|
subscribers: make(map[*tcpSubscriber][]broker.Subscriber),
|
|
|
|
}
|
|
|
|
}
|