web: fix deadlock (#1585)
* web: fix deadlock Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * add web tests Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
b875868a39
commit
414b2ec5f8
@ -111,6 +111,9 @@ func (s *service) run(exit chan bool) {
|
||||
}
|
||||
|
||||
func (s *service) register() error {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
if s.srv == nil {
|
||||
return nil
|
||||
}
|
||||
@ -138,6 +141,9 @@ func (s *service) register() error {
|
||||
}
|
||||
|
||||
func (s *service) deregister() error {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
if s.srv == nil {
|
||||
return nil
|
||||
}
|
||||
@ -280,18 +286,22 @@ func (s *service) Client() *http.Client {
|
||||
|
||||
func (s *service) Handle(pattern string, handler http.Handler) {
|
||||
var seen bool
|
||||
s.RLock()
|
||||
for _, ep := range s.srv.Endpoints {
|
||||
if ep.Name == pattern {
|
||||
seen = true
|
||||
break
|
||||
}
|
||||
}
|
||||
s.RUnlock()
|
||||
|
||||
// if its unseen then add an endpoint
|
||||
if !seen {
|
||||
s.Lock()
|
||||
s.srv.Endpoints = append(s.srv.Endpoints, ®istry.Endpoint{
|
||||
Name: pattern,
|
||||
})
|
||||
s.Unlock()
|
||||
}
|
||||
|
||||
// disable static serving
|
||||
@ -306,17 +316,23 @@ func (s *service) Handle(pattern string, handler http.Handler) {
|
||||
}
|
||||
|
||||
func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
|
||||
|
||||
var seen bool
|
||||
s.RLock()
|
||||
for _, ep := range s.srv.Endpoints {
|
||||
if ep.Name == pattern {
|
||||
seen = true
|
||||
break
|
||||
}
|
||||
}
|
||||
s.RUnlock()
|
||||
|
||||
if !seen {
|
||||
s.Lock()
|
||||
s.srv.Endpoints = append(s.srv.Endpoints, ®istry.Endpoint{
|
||||
Name: pattern,
|
||||
})
|
||||
s.Unlock()
|
||||
}
|
||||
|
||||
// disable static serving
|
||||
@ -331,7 +347,6 @@ func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *
|
||||
|
||||
func (s *service) Init(opts ...Option) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
for _, o := range opts {
|
||||
o(&s.opts)
|
||||
@ -347,6 +362,8 @@ func (s *service) Init(opts ...Option) error {
|
||||
serviceOpts = append(serviceOpts, micro.Registry(s.opts.Registry))
|
||||
}
|
||||
|
||||
s.Unlock()
|
||||
|
||||
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
@ -386,14 +403,19 @@ func (s *service) Init(opts ...Option) error {
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.RLock()
|
||||
// pass in own name and version
|
||||
serviceOpts = append(serviceOpts, micro.Name(s.opts.Name))
|
||||
serviceOpts = append(serviceOpts, micro.Version(s.opts.Version))
|
||||
s.RUnlock()
|
||||
|
||||
s.opts.Service.Init(serviceOpts...)
|
||||
|
||||
s.Lock()
|
||||
srv := s.genSrv()
|
||||
srv.Endpoints = s.srv.Endpoints
|
||||
s.srv = srv
|
||||
s.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
72
web/web_test.go
Normal file
72
web/web_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package web_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/cli/v2"
|
||||
"github.com/micro/go-micro/v2"
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/web"
|
||||
)
|
||||
|
||||
func TestWeb(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println("Test nr", i)
|
||||
testFunc()
|
||||
}
|
||||
}
|
||||
|
||||
func testFunc() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*250)
|
||||
defer cancel()
|
||||
|
||||
s := micro.NewService(
|
||||
micro.Name("test"),
|
||||
micro.Context(ctx),
|
||||
micro.HandleSignal(false),
|
||||
micro.Flags(
|
||||
&cli.StringFlag{
|
||||
Name: "test.timeout",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "test.v",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "test.run",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "test.testlogfile",
|
||||
},
|
||||
),
|
||||
)
|
||||
w := web.NewService(
|
||||
web.MicroService(s),
|
||||
web.Context(ctx),
|
||||
web.HandleSignal(false),
|
||||
)
|
||||
//s.Init()
|
||||
//w.Init()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := s.Run()
|
||||
if err != nil {
|
||||
logger.Errorf("micro run error: %v", err)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := w.Run()
|
||||
if err != nil {
|
||||
logger.Errorf("web run error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
}
|
Loading…
Reference in New Issue
Block a user