dont display unneded error message

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-07-06 19:10:54 +03:00
parent 28d23f388f
commit c1526f70ce

44
http.go
View File

@ -39,14 +39,13 @@ type httpServer struct {
} }
func (h *httpServer) newCodec(ct string) (codec.Codec, error) { func (h *httpServer) newCodec(ct string) (codec.Codec, error) {
h.RLock()
defer h.RUnlock()
if idx := strings.IndexRune(ct, ';'); idx >= 0 { if idx := strings.IndexRune(ct, ';'); idx >= 0 {
ct = ct[:idx] ct = ct[:idx]
} }
h.RLock()
if cf, ok := h.opts.Codecs[ct]; ok { cf, ok := h.opts.Codecs[ct]
h.RUnlock()
if ok {
return cf, nil return cf, nil
} }
return nil, codec.ErrUnknownContentType return nil, codec.ErrUnknownContentType
@ -65,7 +64,6 @@ func (h *httpServer) Init(opts ...server.Option) error {
} }
h.Lock() h.Lock()
defer h.Unlock()
for _, o := range opts { for _, o := range opts {
o(&h.opts) o(&h.opts)
@ -86,6 +84,7 @@ func (h *httpServer) Init(opts ...server.Option) error {
for pp, ph := range phs.h { for pp, ph := range phs.h {
exp, err := regexp.Compile(pp) exp, err := regexp.Compile(pp)
if err != nil { if err != nil {
h.Unlock()
return err return err
} }
h.pathHandlers[exp] = ph h.pathHandlers[exp] = ph
@ -96,52 +95,68 @@ func (h *httpServer) Init(opts ...server.Option) error {
h.contentTypeHandlers[pp] = ph h.contentTypeHandlers[pp] = ph
} }
} }
h.Unlock()
h.RLock()
if err := h.opts.Register.Init(); err != nil { if err := h.opts.Register.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Broker.Init(); err != nil { if err := h.opts.Broker.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Tracer.Init(); err != nil { if err := h.opts.Tracer.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Auth.Init(); err != nil { if err := h.opts.Auth.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Logger.Init(); err != nil { if err := h.opts.Logger.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Meter.Init(); err != nil { if err := h.opts.Meter.Init(); err != nil {
h.RUnlock()
return err return err
} }
if err := h.opts.Transport.Init(); err != nil { if err := h.opts.Transport.Init(); err != nil {
h.RUnlock()
return err return err
} }
h.RUnlock()
h.Lock()
h.init = true h.init = true
h.Unlock()
return nil return nil
} }
func (h *httpServer) Handle(handler server.Handler) error { func (h *httpServer) Handle(handler server.Handler) error {
h.Lock()
defer h.Unlock()
hdlr, ok := handler.(*httpHandler) hdlr, ok := handler.(*httpHandler)
if !ok { if !ok {
h.Lock()
h.hd = handler h.hd = handler
h.Unlock()
return nil return nil
} }
if _, ok := hdlr.hd.(http.Handler); ok { if _, ok := hdlr.hd.(http.Handler); ok {
h.Lock()
h.hd = handler h.hd = handler
h.Unlock()
return nil return nil
} }
h.Lock()
if h.handlers == nil { if h.handlers == nil {
h.handlers = make(map[string]server.Handler) h.handlers = make(map[string]server.Handler)
} }
h.handlers[handler.Name()] = handler h.handlers[handler.Name()] = handler
h.Unlock()
return nil return nil
} }
@ -247,13 +262,15 @@ func (h *httpServer) Subscribe(sb server.Subscriber) error {
return err return err
} }
h.Lock() h.RLock()
defer h.Unlock()
_, ok = h.subscribers[sub] _, ok = h.subscribers[sub]
h.RUnlock()
if ok { if ok {
return fmt.Errorf("subscriber %v already exists", h) return fmt.Errorf("subscriber %v already exists", h)
} }
h.Lock()
h.subscribers[sub] = nil h.subscribers[sub] = nil
h.Unlock()
return nil return nil
} }
@ -318,8 +335,6 @@ func (h *httpServer) Register() error {
} }
h.Lock() h.Lock()
defer h.Unlock()
for sb := range h.subscribers { for sb := range h.subscribers {
handler := h.createSubHandler(sb, config) handler := h.createSubHandler(sb, config)
var opts []broker.SubscribeOption var opts []broker.SubscribeOption
@ -336,6 +351,7 @@ func (h *httpServer) Register() error {
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...) sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
if err != nil { if err != nil {
h.Unlock()
return err return err
} }
h.subscribers[sb] = []broker.Subscriber{sub} h.subscribers[sb] = []broker.Subscriber{sub}
@ -343,6 +359,7 @@ func (h *httpServer) Register() error {
h.registered = true h.registered = true
h.rsvc = service h.rsvc = service
h.Unlock()
return nil return nil
} }
@ -384,6 +401,7 @@ func (h *httpServer) Deregister() error {
for _, sub := range subs { for _, sub := range subs {
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic()) config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic())
if err := sub.Unsubscribe(subCtx); err != nil { if err := sub.Unsubscribe(subCtx); err != nil {
h.Unlock()
config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err) config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err)
return err return err
} }
@ -497,7 +515,7 @@ func (h *httpServer) Start() error {
}() }()
} else { } else {
go func() { go func() {
if cerr := http.Serve(ts, fn); cerr != nil { if cerr := http.Serve(ts, fn); cerr != nil && !strings.Contains(cerr.Error(), "use of closed network connection") {
h.opts.Logger.Error(h.opts.Context, cerr) h.opts.Logger.Error(h.opts.Context, cerr)
} }
}() }()