-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
155 lines (138 loc) · 3.14 KB
/
config.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package goconfig
import (
"github.com/spf13/viper"
"regexp"
"sync"
)
type Config struct {
config *viper.Viper
cLock *sync.RWMutex
kLock *sync.RWMutex
listWatchKey []*keyWatch
listWatchMapKey []*mapKeyWatch
listWatchMatchKey []*matchKeyWatch
}
var _c *Config
func init() {
_c = New()
}
func New() *Config {
return &Config{
config: viper.New(),
cLock: &sync.RWMutex{},
kLock: &sync.RWMutex{},
}
}
func OnKeyChange(key string, fn func()) {
_c.OnKeyChange(key, fn)
}
func OnMapKeyChange(key string, fn func(e ConfigUpdateEvent)) {
_c.OnMapKeyChange(key, fn)
}
func OnMatchKeyChange(key string, fn func(e ConfigUpdateEvent)) {
_c.OnMapKeyChange(key, fn)
}
func (c *Config) OnKeyChange(key string, fn func()) {
c.kLock.Lock()
defer c.kLock.Unlock()
isHit := false
for _, watch := range c.listWatchKey {
if watch.key == key {
watch.notify = append(watch.notify, fn)
isHit = true
}
}
if !isHit {
watch := &keyWatch{
watchItem: acquireWatchItem(),
key: key,
notify: []func(){fn},
config: c,
lock: &sync.Mutex{},
}
watch.init()
c.listWatchKey = append(c.listWatchKey, watch)
}
}
func (c *Config) OnMapKeyChange(key string, fn func(e ConfigUpdateEvent)) {
c.kLock.Lock()
defer c.kLock.Unlock()
isHit := false
for _, watch := range c.listWatchMapKey {
if watch.key == key {
watch.notify = append(watch.notify, fn)
isHit = true
}
}
if !isHit {
mapWatch := &mapKeyWatch{
keyWatch: &keyWatch{
watchItem: acquireWatchItem(),
key: key,
config: c,
lock: &sync.Mutex{},
},
mapWatchItem: make(map[string]*watchItem, 1),
notify: []func(e ConfigUpdateEvent){fn},
}
mapWatch.init()
c.listWatchMapKey = append(c.listWatchMapKey, mapWatch)
}
}
func (c *Config) OnMatchKeyChange(key *regexp.Regexp, fn func(e ConfigUpdateEvent)) {
c.kLock.Lock()
defer c.kLock.Unlock()
isHit := false
for _, watch := range c.listWatchMatchKey {
if watch.key == key || key.String() == watch.key.String() {
watch.notify = append(watch.notify, fn)
isHit = true
}
}
if !isHit {
matchWatch := &matchKeyWatch{
key: key,
config: c,
lock: &sync.Mutex{},
mapWatchItem: make(map[string]*watchItem, 1),
notify: []func(e ConfigUpdateEvent){fn},
}
matchWatch.init()
c.listWatchMatchKey = append(c.listWatchMatchKey, matchWatch)
}
}
func GetConfig() *viper.Viper {
return _c.GetConfig()
}
func (c *Config) GetConfig() *viper.Viper {
c.cLock.RLock()
defer c.cLock.RUnlock()
return c.config
}
func SetConfig(v *viper.Viper) {
_c.SetConfig(v)
}
// SetConfig 重置全局viper
func (c *Config) SetConfig(v *viper.Viper) {
c.cLock.Lock()
c.config = v
c.cLock.Unlock()
if len(c.listWatchKey) > 0 ||
len(c.listWatchMapKey) > 0 ||
len(c.listWatchMatchKey) > 0 {
c.notifyKeyUpdate()
}
}
func (c *Config) notifyKeyUpdate() {
c.kLock.RLock()
defer c.kLock.RUnlock()
for _, watch := range c.listWatchKey {
watch.checkAndNotify()
}
for _, watch := range c.listWatchMapKey {
watch.checkAndNotify()
}
for _, watch := range c.listWatchMatchKey {
watch.checkAndNotify()
}
}