-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_match.go
126 lines (117 loc) · 2.49 KB
/
watch_match.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
117
118
119
120
121
122
123
124
125
126
package goconfig
import (
"regexp"
"strings"
"sync"
)
type matchKeyWatch struct {
lock *sync.Mutex
key *regexp.Regexp
config *Config
mapWatchItem map[string]*watchItem
notify []func(e ConfigUpdateEvent)
}
func (w *matchKeyWatch) init() {
w.reload()
}
func (w *matchKeyWatch) keys() []string {
listKey := make([]string, 0)
cfg := w.config.GetConfig()
if cfg != nil {
keys := cfg.AllKeys()
for _, k := range keys {
kk := keySlice(cfg, k)
listKey = append(listKey, kk...)
}
}
// 展开+去重
mapUnique := make(map[string]bool)
newListKey := make([]string, 0)
for _, k := range listKey {
p := strings.Split(k, ".")
l := len(p)
for i := 0; i < l; i++ {
sub := p[0 : i+1]
newKey := strings.Join(sub, ".")
if !mapUnique[newKey] {
mapUnique[newKey] = true
if w.key.MatchString(newKey) {
newListKey = append(newListKey, newKey)
}
}
}
}
return newListKey
}
func (w *matchKeyWatch) reload() {
listKey := w.keys()
mapWatchItem := make(map[string]*watchItem, len(listKey))
if len(listKey) > 0 {
for _, k := range listKey {
item := acquireWatchItem()
item.key = k
item.config = w.config
item.reload()
mapWatchItem[k] = item
}
}
oldMapWatchItem := w.mapWatchItem
w.mapWatchItem = mapWatchItem
freeWatchItemMap(oldMapWatchItem)
}
func (w *matchKeyWatch) checkAndNotify() {
w.lock.Lock()
defer w.lock.Unlock()
listFn := w.checkKeyAndNotify()
if len(listFn) > 0 {
w.reload()
}
for _, fn := range listFn {
fn()
}
}
func (w *matchKeyWatch) checkKeyAndNotify() []func() {
keys := w.keys()
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 {
// 不存在,则新增
listFn = append(listFn, func(key string) func() {
return func() {
notify(key, key, EventOpAdd)
}
}(k))
}
}
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
}