Skip to content

Commit

Permalink
Merge pull request #788 from LaurenceLiZhixin/fix/consul-destory
Browse files Browse the repository at this point in the history
Fix: consul destory
  • Loading branch information
hxmhlt authored Oct 13, 2020
2 parents b81274a + a407f62 commit 68f7411
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
23 changes: 22 additions & 1 deletion registry/consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (
)

const (
registryConnDelay = 3
registryConnDelay = 3
registryDestroyDefaultTimeout = time.Second * 3
)

func init() {
Expand Down Expand Up @@ -187,5 +188,25 @@ 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 destory with panic: %v", e)
}
done <- struct{}{}
}()
if err := r.UnRegister(*r.URL); err != nil {
logger.Errorf("consul registry unregister with err: %s", err.Error())
}
}()
select {
case <-done:
logger.Infof("consulRegistry unregister done")
case <-time.After(registryDestroyDefaultTimeout):
logger.Errorf("consul unregister timeout")
}
}
close(r.done)
}
16 changes: 16 additions & 0 deletions registry/consul/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@ func (suite *consulRegistryTestSuite) testSubscribe() {
assert.NoError(suite.t, err)
suite.listener = listener
}

func (suite *consulRegistryTestSuite) testDestroy() {
consumerRegistryUrl := newConsumerRegistryUrl(registryHost, registryPort)
consumerRegistry, _ := newConsulRegistry(consumerRegistryUrl)
consulRegistryImp := consumerRegistry.(*consulRegistry)
assert.True(suite.t, consulRegistryImp.IsAvailable())
consulRegistryImp.Destroy()
assert.False(suite.t, consulRegistryImp.IsAvailable())

consumerRegistry, _ = newConsulRegistry(consumerRegistryUrl)
consulRegistryImp = consumerRegistry.(*consulRegistry)
consulRegistryImp.URL = nil
assert.True(suite.t, consulRegistryImp.IsAvailable())
consulRegistryImp.Destroy()
assert.False(suite.t, consulRegistryImp.IsAvailable())
}
2 changes: 2 additions & 0 deletions registry/consul/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func test1(t *testing.T) {
suite.testListener(remoting.EventTypeAdd)
suite.testUnregister()
suite.testListener(remoting.EventTypeDel)
suite.testDestroy()
}

// subscribe -> register -> unregister
Expand All @@ -183,6 +184,7 @@ func test2(t *testing.T) {
suite.testListener(remoting.EventTypeAdd)
suite.testUnregister()
suite.testListener(remoting.EventTypeDel)
suite.testDestroy()
}

func TestConsulRegistry(t *testing.T) {
Expand Down

0 comments on commit 68f7411

Please sign in to comment.