-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
47 lines (40 loc) · 1.04 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package xconfig
import (
"github.com/apolloconfig/agollo/v4/component/log"
"github.com/apolloconfig/agollo/v4/storage"
)
type watcher struct {
appId string
s *appSetter
}
func NewWatcher(s *appSetter, appId string) *watcher {
return &watcher{
appId: appId,
s: s,
}
}
//OnChange 增加变更监控
func (w *watcher) OnChange(event *storage.ChangeEvent) {
if event == nil {
return
}
for key, change := range event.Changes {
log.Infof("OnChange appId : %s, key : %s, value : %+v\n", w.appId, key, change)
if change.ChangeType == storage.DELETED {
if err := w.s.SetValue(key, nil); err != nil {
log.Errorf("OnChange failed, appId : %s, key : %s, error : %v", w.appId, key, err)
}
}
}
}
//OnNewestChange 监控最新变更
func (w *watcher) OnNewestChange(event *storage.FullChangeEvent) {
if event == nil {
return
}
for key, val := range event.Changes {
if err := w.s.SetValue(key, val); err != nil {
log.Errorf("OnNewestChange failed, appId : %s, key : %s, value : %v, error : %v", w.appId, key, val, err)
}
}
}