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
934ad5c4
Commit
934ad5c4
authored
4 years ago
by
tiecheng
Browse files
Options
Downloads
Patches
Plain Diff
remove callback add
watcher error print warn
parent
3ecffc7b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
config_center/file/impl_test.go
+4
-14
4 additions, 14 deletions
config_center/file/impl_test.go
config_center/file/listener.go
+15
-5
15 additions, 5 deletions
config_center/file/listener.go
with
19 additions
and
19 deletions
config_center/file/impl_test.go
+
4
−
14
View file @
934ad5c4
...
...
@@ -76,19 +76,11 @@ func TestAddListener(t *testing.T) {
value
=
"Test Value 2"
err
=
file
.
PublishConfig
(
key
,
group
,
value
)
assert
.
NoError
(
t
,
err
)
listener
.
wg
.
Add
(
1
)
value
=
"Test Value 3"
err
=
file
.
PublishConfig
(
key
,
group
,
value
)
assert
.
NoError
(
t
,
err
)
listener
.
wg
.
Wait
()
time
.
Sleep
(
time
.
Second
)
defer
destroy
(
file
.
rootPath
,
file
)
}
func
Test
AddAnd
RemoveListener
(
t
*
testing
.
T
)
{
func
TestRemoveListener
(
t
*
testing
.
T
)
{
file
,
err
:=
initFileData
(
t
)
group
:=
"dubbogo"
value
:=
"Test Value"
...
...
@@ -103,19 +95,17 @@ func TestAddAndRemoveListener(t *testing.T) {
err
=
file
.
PublishConfig
(
key
,
group
,
value
)
assert
.
NoError
(
t
,
err
)
//
sleep,
make sure callback
run success, do `l.wg.Done()`
// make sure callback
before RemoveListener
time
.
Sleep
(
time
.
Second
)
file
.
RemoveListener
(
key
,
listener
,
config_center
.
WithGroup
(
group
))
listener
.
wg
.
Add
(
1
)
file
.
RemoveListener
(
key
,
listener
,
config_center
.
WithGroup
(
group
))
value
=
"Test Value 3"
err
=
file
.
PublishConfig
(
key
,
group
,
value
)
assert
.
NoError
(
t
,
err
)
listener
.
wg
.
Done
()
listener
.
wg
.
Wait
()
time
.
Sleep
(
time
.
Second
)
defer
destroy
(
file
.
rootPath
,
file
)
}
...
...
@@ -158,8 +148,8 @@ func TestPublishConfig(t *testing.T) {
}
func
destroy
(
path
string
,
fdc
*
FileSystemDynamicConfiguration
)
{
os
.
RemoveAll
(
path
)
fdc
.
Close
()
os
.
RemoveAll
(
path
)
}
type
mockDataListener
struct
{
...
...
This diff is collapsed.
Click to expand it.
config_center/file/listener.go
+
15
−
5
View file @
934ad5c4
...
...
@@ -55,21 +55,21 @@ func NewCacheListener(rootPath string) *CacheListener {
logger
.
Debugf
(
"watcher %s, event %v"
,
cl
.
rootPath
,
event
)
if
event
.
Op
&
fsnotify
.
Write
==
fsnotify
.
Write
{
if
l
,
ok
:=
cl
.
keyListeners
.
Load
(
key
);
ok
{
all
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeUpdate
)
dataChange
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeUpdate
)
}
}
if
event
.
Op
&
fsnotify
.
Create
==
fsnotify
.
Create
{
if
l
,
ok
:=
cl
.
keyListeners
.
Load
(
key
);
ok
{
all
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeAdd
)
dataChange
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeAdd
)
}
}
if
event
.
Op
&
fsnotify
.
Remove
==
fsnotify
.
Remove
{
if
l
,
ok
:=
cl
.
keyListeners
.
Load
(
key
);
ok
{
all
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeDel
)
remove
Callback
(
l
.
(
map
[
config_center
.
ConfigurationListener
]
struct
{}),
key
,
remoting
.
EventTypeDel
)
}
}
case
err
:=
<-
watch
.
Errors
:
logger
.
Error
f
(
"file : listen watch fail:"
,
err
)
logger
.
Warn
f
(
"file : listen watch fail:"
,
err
)
}
}
}()
...
...
@@ -82,7 +82,16 @@ func NewCacheListener(rootPath string) *CacheListener {
return
cl
}
func
allCallback
(
lmap
map
[
config_center
.
ConfigurationListener
]
struct
{},
key
string
,
event
remoting
.
EventType
)
{
func
removeCallback
(
lmap
map
[
config_center
.
ConfigurationListener
]
struct
{},
key
string
,
event
remoting
.
EventType
)
{
if
len
(
lmap
)
==
0
{
logger
.
Warnf
(
"file watch callback but configuration listener is empty, key:%s, event:%v"
,
key
,
event
)
}
for
l
:=
range
lmap
{
callback
(
l
,
key
,
""
,
event
)
}
}
func
dataChangeCallback
(
lmap
map
[
config_center
.
ConfigurationListener
]
struct
{},
key
string
,
event
remoting
.
EventType
)
{
if
len
(
lmap
)
==
0
{
logger
.
Warnf
(
"file watch callback but configuration listener is empty, key:%s, event:%v"
,
key
,
event
)
}
...
...
@@ -105,6 +114,7 @@ func (cl *CacheListener) Close() {
}
// AddListener will add a listener if loaded
// if you watcher a file or directory not exist, will error with no such file or directory
func
(
cl
*
CacheListener
)
AddListener
(
key
string
,
listener
config_center
.
ConfigurationListener
)
{
// reference from https://stackoverflow.com/questions/34018908/golang-why-dont-we-have-a-set-datastructure
// make a map[your type]struct{} like set in java
...
...
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