Skip to content

Commit

Permalink
Merge pull request #973 from LaurenceLiZhixin/fix/consulUnreg
Browse files Browse the repository at this point in the history
Fix/consul unreg
  • Loading branch information
AlexStocks committed Jan 10, 2021
2 parents e633a73 + 082a0bd commit 374a01a
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions registry/consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

import (
getty "github.com/apache/dubbo-getty"
consul "github.com/hashicorp/consul/api"
perrors "github.com/pkg/errors"
)
Expand Down Expand Up @@ -56,6 +57,10 @@ type consulRegistry struct {
// Done field represents whether
// consul registry is closed.
done chan struct{}

// registeredURLs field represents all URLs that have been registered
// will be unregistered when destroyed
registeredURLs []*common.URL
}

func newConsulRegistry(url *common.URL) (registry.Registry, error) {
Expand Down Expand Up @@ -91,6 +96,7 @@ func (r *consulRegistry) Register(url *common.URL) error {

// register actually register the @url
func (r *consulRegistry) register(url *common.URL) error {
r.registeredURLs = append(r.registeredURLs, url.Clone())
service, err := buildService(url)
if err != nil {
return err
Expand Down Expand Up @@ -188,25 +194,26 @@ func (r *consulRegistry) IsAvailable() bool {

// Destroy consul registry center
func (r *consulRegistry) Destroy() {
if r.URL != nil {
done := make(chan struct{}, 1)
go func() {
defer func() {
if e := recover(); e != nil {
logger.Errorf("consulRegistry destroy with panic: %v", e)
}
done <- struct{}{}
}()
if err := r.UnRegister(r.URL); err != nil {
logger.Errorf("consul registry unregister with err: %s", err.Error())
done := make(chan struct{}, 1)
go func() {
defer func() {
if e := recover(); e != nil {
logger.Errorf("consulRegistry destroy with panic: %v", e)
}
done <- struct{}{}
}()
select {
case <-done:
logger.Infof("consulRegistry unregister done")
case <-time.After(registryDestroyDefaultTimeout):
logger.Errorf("consul unregister timeout")
for _, url := range r.registeredURLs {
if err := r.UnRegister(url); err != nil {
logger.Errorf("consul registry unregister with err: %s", err.Error())
}
}
}()
select {
case <-done:
logger.Infof("consulRegistry unregister done")
case <-getty.GetTimeWheel().After(registryDestroyDefaultTimeout):
logger.Errorf("consul unregister timeout")
}

close(r.done)
}

0 comments on commit 374a01a

Please sign in to comment.