2020-09-05 02:11:29 +03:00
|
|
|
package server
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
import (
|
2024-04-22 08:47:50 +03:00
|
|
|
"context"
|
2020-10-16 09:38:57 +03:00
|
|
|
"fmt"
|
2024-04-22 08:47:50 +03:00
|
|
|
"reflect"
|
|
|
|
"runtime/debug"
|
2020-10-16 09:38:57 +03:00
|
|
|
"sort"
|
2024-04-22 08:47:50 +03:00
|
|
|
"strings"
|
2020-10-16 09:38:57 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/errors"
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/options"
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
maddr "go.unistack.org/micro/v3/util/addr"
|
|
|
|
mnet "go.unistack.org/micro/v3/util/net"
|
|
|
|
"go.unistack.org/micro/v3/util/rand"
|
2020-10-16 09:38:57 +03:00
|
|
|
)
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultCodecs will be used to encode/decode
|
|
|
|
var DefaultCodecs = map[string]codec.Codec{
|
|
|
|
"application/octet-stream": codec.NewCodec(),
|
|
|
|
}
|
2020-10-16 09:38:57 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
defaultContentType = "application/json"
|
|
|
|
)
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
type rpcHandler struct {
|
|
|
|
opts HandlerOptions
|
|
|
|
handler interface{}
|
|
|
|
name string
|
|
|
|
endpoints []*register.Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRPCHandler(handler interface{}, opts ...HandlerOption) Handler {
|
|
|
|
options := NewHandlerOptions(opts...)
|
|
|
|
|
|
|
|
typ := reflect.TypeOf(handler)
|
|
|
|
hdlr := reflect.ValueOf(handler)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
|
|
|
var endpoints []*register.Endpoint
|
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
if e := register.ExtractEndpoint(typ.Method(m)); e != nil {
|
|
|
|
e.Name = name + "." + e.Name
|
|
|
|
|
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpcHandler{
|
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Handler() interface{} {
|
|
|
|
return r.handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Endpoints() []*register.Endpoint {
|
|
|
|
return r.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Options() HandlerOptions {
|
|
|
|
return r.opts
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
type noopServer struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
h Handler
|
2021-04-25 11:58:24 +03:00
|
|
|
wg *sync.WaitGroup
|
2021-01-29 13:17:32 +03:00
|
|
|
rsvc *register.Service
|
2020-10-16 09:38:57 +03:00
|
|
|
handlers map[string]Handler
|
|
|
|
subscribers map[*subscriber][]broker.Subscriber
|
|
|
|
exit chan chan error
|
2021-04-25 11:58:24 +03:00
|
|
|
opts Options
|
2020-10-16 09:38:57 +03:00
|
|
|
sync.RWMutex
|
2021-03-06 19:45:13 +03:00
|
|
|
registered bool
|
|
|
|
started bool
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewServer returns new noop server
|
|
|
|
func NewServer(opts ...Option) Server {
|
2021-02-12 16:33:16 +03:00
|
|
|
n := &noopServer{opts: NewOptions(opts...)}
|
|
|
|
if n.handlers == nil {
|
|
|
|
n.handlers = make(map[string]Handler)
|
|
|
|
}
|
|
|
|
if n.subscribers == nil {
|
|
|
|
n.subscribers = make(map[*subscriber][]broker.Subscriber)
|
|
|
|
}
|
|
|
|
if n.exit == nil {
|
|
|
|
n.exit = make(chan chan error)
|
|
|
|
}
|
|
|
|
return n
|
2020-11-03 01:08:23 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopServer) newCodec(contentType string) (codec.Codec, error) {
|
2020-10-16 09:38:57 +03:00
|
|
|
if cf, ok := n.opts.Codecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
2020-11-23 16:18:47 +03:00
|
|
|
return nil, codec.ErrUnknownContentType
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
func (n *noopServer) Live() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopServer) Ready() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopServer) Health() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Handle(handler Handler) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
n.h = handler
|
|
|
|
return nil
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (n *noopServer) Name() string {
|
|
|
|
return n.opts.Name
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Subscribe(sb Subscriber) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
sub, ok := sb.(*subscriber)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid subscriber: expected *subscriber")
|
2021-07-27 23:58:06 +03:00
|
|
|
} else if len(sub.handlers) == 0 {
|
2020-10-16 09:38:57 +03:00
|
|
|
return fmt.Errorf("invalid subscriber: no handler functions")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateSubscriber(sb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
if _, ok = n.subscribers[sub]; ok {
|
|
|
|
n.Unlock()
|
|
|
|
return fmt.Errorf("subscriber %v already exists", sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.subscribers[sub] = nil
|
|
|
|
n.Unlock()
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
type rpcMessage struct {
|
|
|
|
payload interface{}
|
|
|
|
codec codec.Codec
|
|
|
|
header metadata.Metadata
|
|
|
|
topic string
|
|
|
|
contentType string
|
|
|
|
body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) ContentType() string {
|
|
|
|
return r.contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Topic() string {
|
|
|
|
return r.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Body() interface{} {
|
|
|
|
return r.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Header() metadata.Metadata {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Codec() codec.Codec {
|
|
|
|
return r.codec
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler {
|
2021-04-27 08:32:47 +03:00
|
|
|
return newRPCHandler(h, opts...)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) NewSubscriber(topic string, sb interface{}, opts ...SubscriberOption) Subscriber {
|
2020-10-16 09:38:57 +03:00
|
|
|
return newSubscriber(topic, sb, opts...)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Init(opts ...Option) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.handlers == nil {
|
2021-07-27 23:58:06 +03:00
|
|
|
n.handlers = make(map[string]Handler, 1)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
if n.subscribers == nil {
|
2021-07-27 23:58:06 +03:00
|
|
|
n.subscribers = make(map[*subscriber][]broker.Subscriber, 1)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
2021-07-27 23:58:06 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
if n.exit == nil {
|
|
|
|
n.exit = make(chan chan error)
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Options() Options {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.opts
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) String() string {
|
2020-09-05 02:11:29 +03:00
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Register() error {
|
2020-10-16 09:38:57 +03:00
|
|
|
n.RLock()
|
|
|
|
rsvc := n.rsvc
|
|
|
|
config := n.opts
|
|
|
|
n.RUnlock()
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
// if service already filled, reuse it and return early
|
|
|
|
if rsvc != nil {
|
2021-02-14 16:16:01 +03:00
|
|
|
return DefaultRegisterFunc(rsvc, config)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
var err error
|
2021-01-29 13:17:32 +03:00
|
|
|
var service *register.Service
|
2020-10-16 09:38:57 +03:00
|
|
|
var cacheService bool
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
service, err = NewRegisterService(n)
|
2020-10-16 09:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
n.RLock()
|
2021-04-27 08:32:47 +03:00
|
|
|
handlerList := make([]string, 0, len(n.handlers))
|
|
|
|
for n := range n.handlers {
|
2021-04-20 12:39:33 +03:00
|
|
|
handlerList = append(handlerList, n)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
sort.Strings(handlerList)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
subscriberList := make([]*subscriber, 0, len(n.subscribers))
|
2020-10-16 09:38:57 +03:00
|
|
|
for e := range n.subscribers {
|
2021-04-20 12:39:33 +03:00
|
|
|
subscriberList = append(subscriberList, e)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
sort.Slice(subscriberList, func(i, j int) bool {
|
|
|
|
return subscriberList[i].topic > subscriberList[j].topic
|
|
|
|
})
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
endpoints := make([]*register.Endpoint, 0, len(handlerList)+len(subscriberList))
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, h := range handlerList {
|
|
|
|
endpoints = append(endpoints, n.handlers[h].Endpoints()...)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, e := range subscriberList {
|
|
|
|
endpoints = append(endpoints, e.Endpoints()...)
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
service.Nodes[0].Metadata["protocol"] = "noop"
|
2021-04-27 08:32:47 +03:00
|
|
|
service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"]
|
2020-10-16 09:38:57 +03:00
|
|
|
service.Endpoints = endpoints
|
|
|
|
|
|
|
|
n.RLock()
|
|
|
|
registered := n.registered
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
if !registered {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, fmt.Sprintf("register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID))
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// register the service
|
2021-09-28 23:43:43 +03:00
|
|
|
if err = DefaultRegisterFunc(service, config); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// already registered? don't need to register subscribers
|
|
|
|
if registered {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
n.registered = true
|
|
|
|
if cacheService {
|
|
|
|
n.rsvc = service
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Deregister() error {
|
2020-10-16 09:38:57 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
n.RLock()
|
|
|
|
config := n.opts
|
|
|
|
n.RUnlock()
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
service, err := NewRegisterService(n)
|
2020-10-16 09:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, fmt.Sprintf("deregistering node: %s", service.Nodes[0].ID))
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := DefaultDeregisterFunc(service, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
n.rsvc = nil
|
|
|
|
|
|
|
|
if !n.registered {
|
|
|
|
n.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.registered = false
|
|
|
|
|
|
|
|
cx := config.Context
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for sb, subs := range n.subscribers {
|
2022-04-15 15:50:45 +03:00
|
|
|
for idx := range subs {
|
2020-10-16 09:38:57 +03:00
|
|
|
if sb.Options().Context != nil {
|
|
|
|
cx = sb.Options().Context
|
|
|
|
}
|
2022-04-17 16:25:42 +03:00
|
|
|
|
2022-04-17 11:41:49 +03:00
|
|
|
ncx := cx
|
2020-10-16 09:38:57 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(s broker.Subscriber) {
|
|
|
|
defer wg.Done()
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, "unsubscribing from topic: "+s.Topic())
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
2022-04-17 11:41:49 +03:00
|
|
|
if err := s.Unsubscribe(ncx); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "unsubscribing from topic: "+s.Topic(), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 15:50:45 +03:00
|
|
|
}(subs[idx])
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
n.subscribers[sb] = nil
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
n.Unlock()
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Start() error {
|
2020-10-16 09:38:57 +03:00
|
|
|
n.RLock()
|
|
|
|
if n.started {
|
|
|
|
n.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config := n.Options()
|
|
|
|
n.RUnlock()
|
|
|
|
|
2021-07-27 23:58:06 +03:00
|
|
|
// use 127.0.0.1 to avoid scan of all network interfaces
|
|
|
|
addr, err := maddr.Extract("127.0.0.1")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var rng rand.Rand
|
|
|
|
i := rng.Intn(20000)
|
|
|
|
// set addr with port
|
|
|
|
addr = mnet.HostPort(addr, 10000+i)
|
|
|
|
|
|
|
|
config.Address = addr
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, "server [noop] Listening on "+config.Address)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
2021-07-27 23:58:06 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
n.Lock()
|
|
|
|
if len(config.Advertise) == 0 {
|
|
|
|
config.Advertise = config.Address
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
// only connect if we're subscribed
|
|
|
|
if len(n.subscribers) > 0 {
|
|
|
|
// connect to the broker
|
|
|
|
if err := config.Broker.Connect(config.Context); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("broker [%s] connect error", config.Broker.String()), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, fmt.Sprintf("broker [%s] Connected to %s", config.Broker.String(), config.Broker.Address()))
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use RegisterCheck func before register
|
2021-04-27 08:32:47 +03:00
|
|
|
// nolint: nestif
|
2020-10-16 09:38:57 +03:00
|
|
|
if err := config.RegisterCheck(config.Context); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("server %s-%s register check error", config.Name, config.ID), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// announce self to the world
|
|
|
|
if err := n.Register(); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server register error", err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:18:47 +03:00
|
|
|
if err := n.subscribe(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
go func() {
|
|
|
|
t := new(time.Ticker)
|
|
|
|
|
|
|
|
// only process if it exists
|
|
|
|
if config.RegisterInterval > time.Duration(0) {
|
|
|
|
// new ticker
|
|
|
|
t = time.NewTicker(config.RegisterInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return error chan
|
|
|
|
var ch chan error
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// register self on interval
|
|
|
|
case <-t.C:
|
|
|
|
n.RLock()
|
|
|
|
registered := n.registered
|
|
|
|
n.RUnlock()
|
|
|
|
rerr := config.RegisterCheck(config.Context)
|
2021-04-27 08:32:47 +03:00
|
|
|
// nolint: nestif
|
2020-10-16 09:38:57 +03:00
|
|
|
if rerr != nil && registered {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("server %s-%s register check error, deregister it", config.Name, config.ID), rerr)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
// deregister self in case of error
|
|
|
|
if err := n.Deregister(); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("server %s-%s deregister error", config.Name, config.ID), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if rerr != nil && !registered {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-10-12 12:37:43 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("server %s-%s register check error", config.Name, config.ID), rerr)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := n.Register(); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("server %s-%s register error", config.Name, config.ID), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// wait for exit
|
|
|
|
case ch = <-n.exit:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister self
|
|
|
|
if err := n.Deregister(); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server deregister error", err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for waitgroup
|
|
|
|
if n.wg != nil {
|
|
|
|
n.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// close transport
|
|
|
|
ch <- nil
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, fmt.Sprintf("broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address()))
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
// disconnect broker
|
|
|
|
if err := config.Broker.Disconnect(config.Context); err != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Error(n.opts.Context, fmt.Sprintf("broker [%s] disconnect error", config.Broker.String()), err)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// mark the server as started
|
|
|
|
n.Lock()
|
|
|
|
n.started = true
|
|
|
|
n.Unlock()
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:18:47 +03:00
|
|
|
func (n *noopServer) subscribe() error {
|
|
|
|
config := n.Options()
|
|
|
|
|
2024-04-23 07:39:08 +03:00
|
|
|
subCtx := config.Context
|
2023-05-25 23:18:47 +03:00
|
|
|
|
|
|
|
for sb := range n.subscribers {
|
2024-04-23 07:39:08 +03:00
|
|
|
|
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
2023-05-25 23:18:47 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 07:39:08 +03:00
|
|
|
opts := []broker.SubscribeOption{
|
|
|
|
broker.SubscribeContext(subCtx),
|
|
|
|
broker.SubscribeAutoAck(sb.Options().AutoAck),
|
|
|
|
broker.SubscribeBodyOnly(sb.Options().BodyOnly),
|
2023-05-25 23:18:47 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 07:39:08 +03:00
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
|
|
|
opts = append(opts, broker.SubscribeGroup(queue))
|
2023-05-25 23:18:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2024-04-23 07:28:58 +03:00
|
|
|
config.Logger.Info(n.opts.Context, "subscribing to topic: "+sb.Topic())
|
2023-05-25 23:18:47 +03:00
|
|
|
}
|
2024-04-23 07:39:08 +03:00
|
|
|
|
|
|
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), n.createSubHandler(sb, config), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-25 23:18:47 +03:00
|
|
|
|
|
|
|
n.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopServer) Stop() error {
|
2020-10-16 09:38:57 +03:00
|
|
|
n.RLock()
|
|
|
|
if !n.started {
|
|
|
|
n.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
n.exit <- ch
|
|
|
|
|
|
|
|
err := <-ch
|
|
|
|
n.Lock()
|
|
|
|
n.rsvc = nil
|
|
|
|
n.started = false
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
return err
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subscriber {
|
|
|
|
var endpoints []*register.Endpoint
|
|
|
|
var handlers []*handler
|
|
|
|
|
|
|
|
options := NewSubscriberOptions(opts...)
|
|
|
|
|
|
|
|
if typ := reflect.TypeOf(sub); typ.Kind() == reflect.Func {
|
|
|
|
h := &handler{
|
|
|
|
method: reflect.ValueOf(sub),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
h.reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
h.ctxType = typ.In(0)
|
|
|
|
h.reqType = typ.In(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
ep := ®ister.Endpoint{
|
|
|
|
Name: "Func",
|
|
|
|
Request: register.ExtractSubValue(typ),
|
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
|
|
|
} else {
|
|
|
|
hdlr := reflect.ValueOf(sub)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
method := typ.Method(m)
|
|
|
|
h := &handler{
|
|
|
|
method: method.Func,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method.Type.NumIn() {
|
|
|
|
case 2:
|
|
|
|
h.reqType = method.Type.In(1)
|
|
|
|
case 3:
|
|
|
|
h.ctxType = method.Type.In(1)
|
|
|
|
h.reqType = method.Type.In(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
ep := ®ister.Endpoint{
|
|
|
|
Name: name + "." + method.Name,
|
|
|
|
Request: register.ExtractSubValue(method.Type),
|
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handler {
|
|
|
|
return func(p broker.Event) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
n.RLock()
|
|
|
|
config := n.opts
|
|
|
|
n.RUnlock()
|
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
|
|
|
config.Logger.Error(n.opts.Context, "panic recovered: ", r)
|
|
|
|
config.Logger.Error(n.opts.Context, string(debug.Stack()))
|
|
|
|
}
|
|
|
|
err = errors.InternalServerError(n.opts.Name+".subscriber", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
msg := p.Message()
|
|
|
|
// if we don't have headers, create empty map
|
|
|
|
if msg.Header == nil {
|
|
|
|
msg.Header = metadata.New(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
if len(ct) == 0 {
|
|
|
|
msg.Header.Set(metadata.HeaderContentType, defaultContentType)
|
|
|
|
ct = defaultContentType
|
|
|
|
}
|
|
|
|
cf, err := n.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := metadata.New(len(msg.Header))
|
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := metadata.NewIncomingContext(sb.opts.Context, hdr)
|
|
|
|
|
|
|
|
results := make(chan error, len(sb.handlers))
|
|
|
|
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
handler := sb.handlers[i]
|
|
|
|
|
|
|
|
var isVal bool
|
|
|
|
var req reflect.Value
|
|
|
|
|
|
|
|
if handler.reqType.Kind() == reflect.Ptr {
|
|
|
|
req = reflect.New(handler.reqType.Elem())
|
|
|
|
} else {
|
|
|
|
req = reflect.New(handler.reqType)
|
|
|
|
isVal = true
|
|
|
|
}
|
|
|
|
if isVal {
|
|
|
|
req = req.Elem()
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:41:36 +03:00
|
|
|
if err = cf.Unmarshal(msg.Body, req.Interface()); err != nil {
|
2024-04-22 08:47:50 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(ctx context.Context, msg Message) error {
|
|
|
|
var vals []reflect.Value
|
|
|
|
if sb.typ.Kind() != reflect.Func {
|
|
|
|
vals = append(vals, sb.rcvr)
|
|
|
|
}
|
|
|
|
if handler.ctxType != nil {
|
|
|
|
vals = append(vals, reflect.ValueOf(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, reflect.ValueOf(msg.Body()))
|
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
return rerr.(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
if h, ok := hook.(HookSubHandler); ok {
|
|
|
|
fn = h(fn)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if n.wg != nil {
|
|
|
|
n.wg.Add(1)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if n.wg != nil {
|
|
|
|
defer n.wg.Done()
|
|
|
|
}
|
|
|
|
cerr := fn(ctx, &rpcMessage{
|
|
|
|
topic: sb.topic,
|
|
|
|
contentType: ct,
|
|
|
|
payload: req.Interface(),
|
|
|
|
header: msg.Header,
|
|
|
|
})
|
|
|
|
results <- cerr
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
var errors []string
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
if rerr := <-results; rerr != nil {
|
|
|
|
errors = append(errors, rerr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
err = fmt.Errorf("subscriber error: %s", strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Endpoints() []*register.Endpoint {
|
|
|
|
return s.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
2024-12-09 13:06:43 +03:00
|
|
|
topic string
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
2024-12-09 13:06:43 +03:00
|
|
|
|
|
|
|
endpoints []*register.Endpoint
|
|
|
|
handlers []*handler
|
|
|
|
|
|
|
|
rcvr reflect.Value
|
|
|
|
opts SubscriberOptions
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
|
|
|
method reflect.Value
|
|
|
|
}
|