fix build

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-05-09 18:47:56 +03:00
parent 4a9cc0f03f
commit 2ed04e3e24
3 changed files with 4 additions and 102 deletions

100
http.go
View File

@ -9,12 +9,10 @@ import (
"net" "net"
"net/http" "net/http"
"reflect" "reflect"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
"go.unistack.org/micro/v4/broker"
"go.unistack.org/micro/v4/codec" "go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v4/logger" "go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/register" "go.unistack.org/micro/v4/register"
@ -30,7 +28,6 @@ type Server struct {
rsvc *register.Service rsvc *register.Service
handlers map[string]server.Handler handlers map[string]server.Handler
exit chan chan error exit chan chan error
subscribers map[*httpSubscriber][]broker.Subscriber
errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int) errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int)
pathHandlers *rhttp.Trie pathHandlers *rhttp.Trie
opts server.Options opts server.Options
@ -101,10 +98,6 @@ func (h *Server) Init(opts ...server.Option) error {
h.RUnlock() h.RUnlock()
return err return err
} }
if err := h.opts.Broker.Init(); err != nil {
h.RUnlock()
return err
}
if err := h.opts.Tracer.Init(); err != nil { if err := h.opts.Tracer.Init(); err != nil {
h.RUnlock() h.RUnlock()
return err return err
@ -288,35 +281,6 @@ func (h *Server) NewHandler(handler interface{}, opts ...server.HandlerOption) s
return hdlr return hdlr
} }
func (h *Server) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber {
return newSubscriber(topic, handler, opts...)
}
func (h *Server) Subscribe(sb server.Subscriber) error {
sub, ok := sb.(*httpSubscriber)
if !ok {
return fmt.Errorf("invalid subscriber: expected *httpSubscriber")
}
if len(sub.handlers) == 0 {
return fmt.Errorf("invalid subscriber: no handler functions")
}
if err := server.ValidateSubscriber(sb); err != nil {
return err
}
h.RLock()
_, ok = h.subscribers[sub]
h.RUnlock()
if ok {
return fmt.Errorf("subscriber %v already exists", h)
}
h.Lock()
h.subscribers[sub] = nil
h.Unlock()
return nil
}
func (h *Server) Register() error { func (h *Server) Register() error {
var eps []*register.Endpoint var eps []*register.Endpoint
h.RLock() h.RLock()
@ -342,21 +306,6 @@ func (h *Server) Register() error {
service.Nodes[0].Metadata["protocol"] = "http" service.Nodes[0].Metadata["protocol"] = "http"
service.Endpoints = eps service.Endpoints = eps
h.Lock()
subscriberList := make([]*httpSubscriber, 0, len(h.subscribers))
for e := range h.subscribers {
// Only advertise non internal subscribers
subscriberList = append(subscriberList, e)
}
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()
h.RLock() h.RLock()
registered := h.registered registered := h.registered
h.RUnlock() h.RUnlock()
@ -378,29 +327,6 @@ func (h *Server) Register() error {
} }
h.Lock() h.Lock()
for sb := range h.subscribers {
handler := h.createSubHandler(sb, config)
var opts []broker.SubscribeOption
if queue := sb.Options().Queue; len(queue) > 0 {
opts = append(opts, broker.SubscribeGroup(queue))
}
subCtx := config.Context
if cx := sb.Options().Context; cx != nil {
subCtx = cx
}
opts = append(opts, broker.SubscribeContext(subCtx))
opts = append(opts, broker.SubscribeAutoAck(sb.Options().AutoAck))
opts = append(opts, broker.SubscribeBodyOnly(sb.Options().BodyOnly))
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
if err != nil {
h.Unlock()
return err
}
h.subscribers[sb] = []broker.Subscriber{sub}
}
h.registered = true h.registered = true
h.rsvc = service h.rsvc = service
h.Unlock() h.Unlock()
@ -435,23 +361,6 @@ func (h *Server) Deregister() error {
} }
h.registered = false h.registered = false
subCtx := h.opts.Context
for sb, subs := range h.subscribers {
if cx := sb.Options().Context; cx != nil {
subCtx = cx
}
for _, sub := range subs {
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic())
if err := sub.Unsubscribe(subCtx); err != nil {
h.Unlock()
config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err)
return err
}
}
h.subscribers[sb] = nil
}
h.Unlock() h.Unlock()
return nil return nil
} }
@ -525,10 +434,6 @@ func (h *Server) Start() error {
return fmt.Errorf("cant process with nil handler") return fmt.Errorf("cant process with nil handler")
} }
if err := config.Broker.Connect(h.opts.Context); err != nil {
return err
}
if err := config.RegisterCheck(h.opts.Context); err != nil { if err := config.RegisterCheck(h.opts.Context); err != nil {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err) config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err)
@ -626,10 +531,6 @@ func (h *Server) Start() error {
config.Logger.Errorf(config.Context, "Server deregister error: %s", err) config.Logger.Errorf(config.Context, "Server deregister error: %s", err)
} }
if err := config.Broker.Disconnect(config.Context); err != nil {
config.Logger.Errorf(config.Context, "Broker disconnect error: %s", err)
}
ch <- ts.Close() ch <- ts.Close()
}() }()
@ -659,7 +560,6 @@ func NewServer(opts ...server.Option) *Server {
return &Server{ return &Server{
opts: options, opts: options,
exit: make(chan chan error), exit: make(chan chan error),
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
errorHandler: eh, errorHandler: eh,
pathHandlers: rhttp.NewTrie(), pathHandlers: rhttp.NewTrie(),
} }

View File

@ -85,8 +85,8 @@ func Middleware(mw ...func(http.Handler) http.Handler) server.Option {
type serverKey struct{} type serverKey struct{}
// Server provide ability to pass *http.Server // HTTPServer provide ability to pass *http.Server
func Server(hs *http.Server) server.Option { func HTTPServer(hs *http.Server) server.Option {
return server.SetOption(serverKey{}, hs) return server.SetOption(serverKey{}, hs)
} }

View File

@ -10,6 +10,8 @@ import (
"go.unistack.org/micro/v4/server" "go.unistack.org/micro/v4/server"
) )
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
type methodType struct { type methodType struct {
ArgType reflect.Type ArgType reflect.Type
ReplyType reflect.Type ReplyType reflect.Type