2020-09-05 02:11:29 +03:00
|
|
|
package server
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"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"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "unsubscribing from topic: %s", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "unsubscribing from topic: %s err: %v", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "server [noop] Listening on %s", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "broker [%s] connect error: %v", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server register error: %v", 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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s, 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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server %s-%s deregister error: %s", 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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", 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) {
|
2021-04-27 08:32:47 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "server %s-%s register error: %s", 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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Errorf(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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Infof(n.opts.Context, "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) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Errorf(n.opts.Context, "broker [%s] disconnect error: %v", 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()
|
|
|
|
|
|
|
|
cx := config.Context
|
|
|
|
var err error
|
|
|
|
var sub broker.Subscriber
|
|
|
|
|
|
|
|
for sb := range n.subscribers {
|
|
|
|
if sb.Options().Context != nil {
|
|
|
|
cx = sb.Options().Context
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []broker.SubscribeOption{broker.SubscribeContext(cx), broker.SubscribeAutoAck(sb.Options().AutoAck)}
|
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
|
|
|
opts = append(opts, broker.SubscribeGroup(queue))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sb.Options().Batch {
|
|
|
|
// batch processing handler
|
|
|
|
sub, err = config.Broker.BatchSubscribe(cx, sb.Topic(), n.createBatchSubHandler(sb, config), opts...)
|
|
|
|
} else {
|
|
|
|
// single processing handler
|
|
|
|
sub, err = config.Broker.Subscribe(cx, sb.Topic(), n.createSubHandler(sb, config), opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
|
|
|
config.Logger.Infof(n.opts.Context, "subscribing to topic: %s", sb.Topic())
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|