Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
366911db
Commit
366911db
authored
4 years ago
by
382673304@qq.com
Browse files
Options
Downloads
Patches
Plain Diff
fix: add consul destroy timeout and test file
parent
d226d63d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
registry/consul/registry.go
+24
-2
24 additions, 2 deletions
registry/consul/registry.go
registry/consul/registry_test.go
+16
-0
16 additions, 0 deletions
registry/consul/registry_test.go
registry/consul/utils_test.go
+4
-0
4 additions, 0 deletions
registry/consul/utils_test.go
with
44 additions
and
2 deletions
registry/consul/registry.go
+
24
−
2
View file @
366911db
...
...
@@ -37,6 +37,7 @@ import (
const
(
registryConnDelay
=
3
registryDestroyDefaultTimeout
=
time
.
Second
)
func
init
()
{
...
...
@@ -55,6 +56,9 @@ type consulRegistry struct {
// Done field represents whether
// consul registry is closed.
done
chan
struct
{}
// time wait when destroy
timeOut
time
.
Duration
}
func
newConsulRegistry
(
url
*
common
.
URL
)
(
registry
.
Registry
,
error
)
{
...
...
@@ -68,6 +72,7 @@ func newConsulRegistry(url *common.URL) (registry.Registry, error) {
URL
:
url
,
client
:
client
,
done
:
make
(
chan
struct
{}),
timeOut
:
registryDestroyDefaultTimeout
,
}
return
r
,
nil
...
...
@@ -188,8 +193,25 @@ func (r *consulRegistry) IsAvailable() bool {
// Destroy consul registry center
func
(
r
*
consulRegistry
)
Destroy
()
{
if
r
.
URL
!=
nil
{
if
err
:=
r
.
UnRegister
(
*
r
.
URL
);
err
!=
nil
{
logger
.
Errorf
(
"consul registry unregister with err: %s"
,
err
.
Error
())
done
:=
make
(
chan
struct
{},
1
)
ticker
:=
time
.
NewTicker
(
r
.
timeOut
)
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
<-
ticker
.
C
:
logger
.
Errorf
(
"consul unregister timeout"
)
ticker
.
Stop
()
}
}
close
(
r
.
done
)
...
...
This diff is collapsed.
Click to expand it.
registry/consul/registry_test.go
+
16
−
0
View file @
366911db
...
...
@@ -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
())
}
This diff is collapsed.
Click to expand it.
registry/consul/utils_test.go
+
4
−
0
View file @
366911db
...
...
@@ -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,8 +184,11 @@ func test2(t *testing.T) {
suite
.
testListener
(
remoting
.
EventTypeAdd
)
suite
.
testUnregister
()
suite
.
testListener
(
remoting
.
EventTypeDel
)
suite
.
testDestroy
()
}
func
TestConsulRegistry
(
t
*
testing
.
T
)
{
t
.
Run
(
"test1"
,
test1
)
t
.
Run
(
"test2"
,
test2
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment