1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

fix: "Solve the problem that the resources have not been fully released due to early exit" (#2168)

* Update http.go

Exit before deregister is executed

* Create http.go

Exit before deregister is executed

* Solve the problem that the resources have not been fully released due to early exit

* Optimize some code

* Optimize some code
This commit is contained in:
JeffreyBool 2021-05-17 15:16:52 +08:00 committed by GitHub
parent 4c1f81dadb
commit f48911d2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 791 additions and 126 deletions

48
go.mod
View File

@ -5,29 +5,41 @@ go 1.13
replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.8
require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/Microsoft/hcsshim v0.8.17 // indirect
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/ef-ds/deque v1.0.4-0.20190904040645-54cb57c252a1
github.com/evanphx/json-patch/v5 v5.0.0
github.com/docker/docker v20.10.6+incompatible // indirect
github.com/ef-ds/deque v1.0.4
github.com/evanphx/json-patch/v5 v5.3.0
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/fsouza/go-dockerclient v1.6.0
github.com/go-acme/lego/v3 v3.4.0
github.com/go-git/go-git/v5 v5.1.0
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee
github.com/fsouza/go-dockerclient v1.7.2
github.com/go-acme/lego/v3 v3.9.0
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.3.0
github.com/gobwas/httphead v0.1.0
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.0.3
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.1
github.com/gorilla/handlers v1.4.2
github.com/imdario/mergo v0.3.9
github.com/gobwas/ws v1.0.4
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.2.0
github.com/gorilla/handlers v1.5.1
github.com/imdario/mergo v0.3.12
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/micro/cli/v2 v2.1.2
github.com/miekg/dns v1.1.27
github.com/nxadm/tail v1.4.4
github.com/miekg/dns v1.1.42
github.com/nxadm/tail v1.4.8
github.com/opencontainers/runc v1.0.0-rc94 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.6.1
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/net v0.0.0-20210510120150-4163338589ed
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
)

813
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -915,7 +915,10 @@ func (g *grpcServer) Start() error {
}
// return error chan
var ch chan error
var (
err error
ch chan error
)
Loop:
for {
@ -959,18 +962,18 @@ func (g *grpcServer) Start() error {
g.srv.Stop()
}
// close transport
ch <- nil
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
logger.Infof("Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address())
}
// disconnect broker
if err := config.Broker.Disconnect(); err != nil {
if err = config.Broker.Disconnect(); err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Errorf("Broker [%s] disconnect error: %v", config.Broker.String(), err)
}
}
// close transport
ch <- err
}()
// mark the server as started

View File

@ -13,13 +13,13 @@ type registrySelector struct {
}
func (c *registrySelector) newCache() cache.Cache {
ropts := []cache.Option{}
opts := make([]cache.Option, 0, 1)
if c.so.Context != nil {
if t, ok := c.so.Context.Value("selector_ttl").(time.Duration); ok {
ropts = append(ropts, cache.WithTTL(t))
opts = append(opts, cache.WithTTL(t))
}
}
return cache.New(c.so.Registry, ropts...)
return cache.New(c.so.Registry, opts...)
}
func (c *registrySelector) Init(opts ...Option) error {

View File

@ -38,10 +38,13 @@ func newService(opts ...Option) Service {
options.Client = wrapper.TraceCall(serviceName, trace.DefaultTracer, options.Client)
// wrap the server to provide handler stats
options.Server.Init(
err := options.Server.Init(
server.WrapHandler(wrapper.HandlerStats(stats.DefaultStats)),
server.WrapHandler(wrapper.TraceHandler(trace.DefaultTracer)),
)
if err != nil {
logger.Fatal(err)
}
// set opts
service.opts = options
@ -104,7 +107,10 @@ func (s *service) Init(opts ...Option) {
// Explicitly set the table name to the service name
name := s.opts.Cmd.App().Name
s.opts.Store.Init(store.Table(name))
err := s.opts.Store.Init(store.Table(name))
if err != nil {
logger.Fatal(err)
}
})
}
@ -145,28 +151,24 @@ func (s *service) Start() error {
}
func (s *service) Stop() error {
var gerr error
var err error
for _, fn := range s.opts.BeforeStop {
if err := fn(); err != nil {
gerr = err
}
err = fn()
}
if err := s.opts.Server.Stop(); err != nil {
if err = s.opts.Server.Stop(); err != nil {
return err
}
for _, fn := range s.opts.AfterStop {
if err := fn(); err != nil {
gerr = err
}
err = fn()
}
return gerr
return err
}
func (s *service) Run() error {
func (s *service) Run() (err error) {
// register the debug handler
s.opts.Server.Handle(
s.opts.Server.NewHandler(
@ -182,17 +184,22 @@ func (s *service) Run() error {
// to view blocking profile
rtime.SetBlockProfileRate(1)
if err := s.opts.Profile.Start(); err != nil {
if err = s.opts.Profile.Start(); err != nil {
return err
}
defer s.opts.Profile.Stop()
defer func() {
err = s.opts.Profile.Stop()
if err != nil {
logger.Error(err)
}
}()
}
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
logger.Infof("Starting [service] %s", s.Name())
}
if err := s.Start(); err != nil {
if err = s.Start(); err != nil {
return err
}