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:
Василий Толстов 2020-04-28 14:23:52 +03:00 committed by GitHub
parent b875868a39
commit 414b2ec5f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 1 deletions

View File

@ -111,6 +111,9 @@ func (s *service) run(exit chan bool) {
} }
func (s *service) register() error { func (s *service) register() error {
s.RLock()
defer s.RUnlock()
if s.srv == nil { if s.srv == nil {
return nil return nil
} }
@ -138,6 +141,9 @@ func (s *service) register() error {
} }
func (s *service) deregister() error { func (s *service) deregister() error {
s.RLock()
defer s.RUnlock()
if s.srv == nil { if s.srv == nil {
return nil return nil
} }
@ -280,18 +286,22 @@ func (s *service) Client() *http.Client {
func (s *service) Handle(pattern string, handler http.Handler) { func (s *service) Handle(pattern string, handler http.Handler) {
var seen bool var seen bool
s.RLock()
for _, ep := range s.srv.Endpoints { for _, ep := range s.srv.Endpoints {
if ep.Name == pattern { if ep.Name == pattern {
seen = true seen = true
break break
} }
} }
s.RUnlock()
// if its unseen then add an endpoint // if its unseen then add an endpoint
if !seen { if !seen {
s.Lock()
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{ s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern, Name: pattern,
}) })
s.Unlock()
} }
// disable static serving // 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)) { func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
var seen bool var seen bool
s.RLock()
for _, ep := range s.srv.Endpoints { for _, ep := range s.srv.Endpoints {
if ep.Name == pattern { if ep.Name == pattern {
seen = true seen = true
break break
} }
} }
s.RUnlock()
if !seen { if !seen {
s.Lock()
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{ s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern, Name: pattern,
}) })
s.Unlock()
} }
// disable static serving // disable static serving
@ -331,7 +347,6 @@ func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *
func (s *service) Init(opts ...Option) error { func (s *service) Init(opts ...Option) error {
s.Lock() s.Lock()
defer s.Unlock()
for _, o := range opts { for _, o := range opts {
o(&s.opts) o(&s.opts)
@ -347,6 +362,8 @@ func (s *service) Init(opts ...Option) error {
serviceOpts = append(serviceOpts, micro.Registry(s.opts.Registry)) serviceOpts = append(serviceOpts, micro.Registry(s.opts.Registry))
} }
s.Unlock()
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error { serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
@ -386,14 +403,19 @@ func (s *service) Init(opts ...Option) error {
return nil return nil
})) }))
s.RLock()
// pass in own name and version // pass in own name and version
serviceOpts = append(serviceOpts, micro.Name(s.opts.Name)) serviceOpts = append(serviceOpts, micro.Name(s.opts.Name))
serviceOpts = append(serviceOpts, micro.Version(s.opts.Version)) serviceOpts = append(serviceOpts, micro.Version(s.opts.Version))
s.RUnlock()
s.opts.Service.Init(serviceOpts...) s.opts.Service.Init(serviceOpts...)
s.Lock()
srv := s.genSrv() srv := s.genSrv()
srv.Endpoints = s.srv.Endpoints srv.Endpoints = s.srv.Endpoints
s.srv = srv s.srv = srv
s.Unlock()
return nil return nil
} }

72
web/web_test.go Normal file
View 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()
}