-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_map.go
116 lines (107 loc) · 2.4 KB
/
watch_map.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package goconfig
import (
"fmt"
"reflect"
)
type mapKeyWatch struct {
*keyWatch
mapWatchItem map[string]*watchItem
notify []func(e ConfigUpdateEvent)
}
func (w *mapKeyWatch) init() {
w.reload()
}
func (w *mapKeyWatch) reload() {
w.keyWatch.reload()
listKey := w.keys(w.lastVal)
mapWatchItem := make(map[string]*watchItem, len(listKey))
if len(listKey) > 0 {
for _, k := range listKey {
item := acquireWatchItem()
item.key = fmt.Sprintf("%s.%s", w.key, k)
item.config = w.config
item.reload()
mapWatchItem[k] = item
}
}
oldMapWatchItem := w.mapWatchItem
w.mapWatchItem = mapWatchItem
freeWatchItemMap(oldMapWatchItem)
}
func (w *mapKeyWatch) keys(v interface{}) []string {
var listKey []string
if v == nil {
return listKey
}
switch reflect.TypeOf(v).Kind() {
case reflect.Map:
keys := reflect.ValueOf(v).MapKeys()
listKey = make([]string, 0, len(keys))
for _, k := range keys {
listKey = append(listKey, fmt.Sprintf("%v", k.Interface()))
}
case reflect.Slice, reflect.Array:
maxLen := reflect.ValueOf(v).Len()
listKey = make([]string, 0, maxLen)
for i := 0; i < maxLen; i++ {
listKey = append(listKey, fmt.Sprintf("%d", i))
}
}
return listKey
}
func (w *mapKeyWatch) checkAndNotify() {
w.lock.Lock()
defer w.lock.Unlock()
if w.isChange() {
listFn := w.checkKeyAndNotify()
w.reload()
for _, fn := range listFn {
fn()
}
}
}
func (w *mapKeyWatch) checkKeyAndNotify() []func() {
config := w.config.GetConfig().Get(w.key)
keys := w.keys(config)
hitKey := make(map[string]bool, len(keys))
notify := func(key string, subKey string, op int8) {
event := ConfigUpdateEvent{
fullKey: key,
key: subKey,
op: op,
}
for _, fn := range w.notify {
go fn(event)
}
}
listFn := make([]func(), 0)
for _, k := range keys {
hitKey[k] = true
if item, ok := w.mapWatchItem[k]; ok {
// 存在,检查变更
if item.isChange() {
listFn = append(listFn, func() {
notify(item.key, k, EventOpUpdate)
})
}
} else {
// 不存在,则新增
key := fmt.Sprintf("%s.%s", w.key, k)
listFn = append(listFn, func() {
notify(key, k, EventOpAdd)
})
}
}
for k, item := range w.mapWatchItem {
if hitKey[k] {
continue
}
// 没有命中,则移除
listFn = append(listFn, func(subKey string, key string) func() {
return func() {
notify(key, subKey, EventOpDelete)
}
}(k, item.key))
}
return listFn
}