2020-09-05 02:11:29 +03:00
|
|
|
package server
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
import (
|
2023-05-09 20:04:15 +03:00
|
|
|
"reflect"
|
2020-10-16 09:38:57 +03:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/register"
|
|
|
|
maddr "go.unistack.org/micro/v4/util/addr"
|
|
|
|
mnet "go.unistack.org/micro/v4/util/net"
|
|
|
|
"go.unistack.org/micro/v4/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
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
type noopServer struct {
|
2023-07-29 00:40:58 +03:00
|
|
|
h *rpcHandler
|
2023-05-09 20:04:15 +03:00
|
|
|
wg *sync.WaitGroup
|
|
|
|
rsvc *register.Service
|
2023-07-29 00:40:58 +03:00
|
|
|
handlers map[string]*rpcHandler
|
2023-05-09 20:04:15 +03:00
|
|
|
exit chan chan error
|
|
|
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewServer(opts ...options.Option) Server {
|
2021-02-12 16:33:16 +03:00
|
|
|
n := &noopServer{opts: NewOptions(opts...)}
|
|
|
|
if n.handlers == nil {
|
2023-07-29 00:40:58 +03:00
|
|
|
n.handlers = make(map[string]*rpcHandler)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
if n.exit == nil {
|
|
|
|
n.exit = make(chan chan error)
|
|
|
|
}
|
|
|
|
return n
|
2020-11-03 01:08:23 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (n *noopServer) Handle(h interface{}, opts ...options.Option) error {
|
|
|
|
n.h = newRPCHandler(h, opts...)
|
2020-10-16 09:38:57 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (n *noopServer) Init(opts ...options.Option) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.handlers == nil {
|
2023-07-29 00:40:58 +03:00
|
|
|
n.handlers = make(map[string]*rpcHandler, 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)
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
endpoints := make([]*register.Endpoint, 0, len(handlerList))
|
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
|
|
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Info(n.opts.Context, "register ["+config.Register.String()+"] Registering node: "+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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Info(n.opts.Context, "deregistering node: "+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
|
|
|
|
|
|
|
|
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) {
|
2023-10-14 18:59:27 +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()
|
|
|
|
|
|
|
|
// 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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server "+config.Name+"-"+config.ID+" register check error: "+err.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server register error: "+err.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server "+config.Name+"-"+config.ID+" register check error: ", rerr.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server "+config.Name+"-"+config.ID+" deregister error: ", err.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server "+config.Name+"-"+config.ID+" register check error: ", rerr.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server "+config.Name+"-"+config.ID+" register error: ", err.Error())
|
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) {
|
2023-10-14 18:59:27 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "server deregister error: "+err.Error())
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for waitgroup
|
|
|
|
if n.wg != nil {
|
|
|
|
n.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// close transport
|
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
// mark the server as started
|
|
|
|
n.Lock()
|
|
|
|
n.started = true
|
|
|
|
n.Unlock()
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
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
|
|
|
}
|
2023-05-09 20:04:15 +03:00
|
|
|
|
|
|
|
type rpcHandler struct {
|
2023-07-29 00:40:58 +03:00
|
|
|
opts HandleOptions
|
2023-05-09 20:04:15 +03:00
|
|
|
handler interface{}
|
|
|
|
name string
|
|
|
|
endpoints []*register.Endpoint
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func newRPCHandler(handler interface{}, opts ...options.Option) *rpcHandler {
|
|
|
|
options := NewHandleOptions(opts...)
|
2023-05-09 20:04:15 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (r *rpcHandler) Options() HandleOptions {
|
2023-05-09 20:04:15 +03:00
|
|
|
return r.opts
|
|
|
|
}
|