From 6f12982fcc1bcba2d3fc2e13657bd58ead95beef Mon Sep 17 00:00:00 2001 From: "vito.he" Date: Tue, 13 Oct 2020 21:53:08 +0800 Subject: [PATCH] Merge pull request #788 from LaurenceLiZhixin/fix/consul-destory Fix: consul destory --- registry/consul/registry.go | 23 ++++++++++++++++++++++- registry/consul/registry_test.go | 16 ++++++++++++++++ registry/consul/utils_test.go | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/registry/consul/registry.go b/registry/consul/registry.go index c425c5ec20..b92e335fdb 100644 --- a/registry/consul/registry.go +++ b/registry/consul/registry.go @@ -36,7 +36,8 @@ import ( ) const ( - registryConnDelay = 3 + registryConnDelay = 3 + registryDestroyDefaultTimeout = time.Second * 3 ) func init() { @@ -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) } diff --git a/registry/consul/registry_test.go b/registry/consul/registry_test.go index 94718f5ab6..b300f7536d 100644 --- a/registry/consul/registry_test.go +++ b/registry/consul/registry_test.go @@ -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()) +} diff --git a/registry/consul/utils_test.go b/registry/consul/utils_test.go index 939352dc08..0e5bffe457 100644 --- a/registry/consul/utils_test.go +++ b/registry/consul/utils_test.go @@ -163,6 +163,7 @@ func test1(t *testing.T) { suite.testListener(remoting.EventTypeAdd) suite.testUnregister() suite.testListener(remoting.EventTypeDel) + suite.testDestroy() } // subscribe -> register -> unregister @@ -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) {