Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: consul destory #788

Merged
merged 8 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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