forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_watch.go
180 lines (154 loc) · 4.27 KB
/
store_watch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package store
import (
"fmt"
"strings"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/fagongzi/gateway/pkg/pb/metapb"
"github.com/fagongzi/log"
"github.com/fagongzi/util/format"
"github.com/fagongzi/util/protoc"
)
// Watch watch event from etcd
func (e *EtcdStore) Watch(evtCh chan *Evt, stopCh chan bool) error {
e.evtCh = evtCh
log.Infof("watch event at: <%s>",
e.prefix)
e.doWatch()
return nil
}
func (e *EtcdStore) doWatch() {
watcher := clientv3.NewWatcher(e.rawClient)
defer watcher.Close()
ctx := e.rawClient.Ctx()
for {
rch := watcher.Watch(ctx, e.prefix, clientv3.WithPrefix())
for wresp := range rch {
if wresp.Canceled {
return
}
for _, ev := range wresp.Events {
var evtSrc EvtSrc
var evtType EvtType
switch ev.Type {
case mvccpb.DELETE:
evtType = EventTypeDelete
case mvccpb.PUT:
if ev.IsCreate() {
evtType = EventTypeNew
} else if ev.IsModify() {
evtType = EventTypeUpdate
}
}
key := string(ev.Kv.Key)
if strings.HasPrefix(key, e.clustersDir) {
evtSrc = EventSrcCluster
} else if strings.HasPrefix(key, e.serversDir) {
evtSrc = EventSrcServer
} else if strings.HasPrefix(key, e.bindsDir) {
evtSrc = EventSrcBind
} else if strings.HasPrefix(key, e.apisDir) {
evtSrc = EventSrcAPI
} else if strings.HasPrefix(key, e.routingsDir) {
evtSrc = EventSrcRouting
} else if strings.HasPrefix(key, e.proxiesDir) {
evtSrc = EventSrcProxy
} else {
continue
}
log.Debugf("watch event: <%s, %v>",
key,
evtType)
e.evtCh <- e.watchMethodMapping[evtSrc](evtType, ev.Kv)
}
}
select {
case <-ctx.Done():
// server closed, return
return
default:
}
}
}
func (e *EtcdStore) doWatchWithCluster(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
value := &metapb.Cluster{}
if len(kv.Value) > 0 {
protoc.MustUnmarshal(value, []byte(kv.Value))
}
return &Evt{
Src: EventSrcCluster,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.clustersDir), "", 1),
Value: value,
}
}
func (e *EtcdStore) doWatchWithServer(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
value := &metapb.Server{}
if len(kv.Value) > 0 {
protoc.MustUnmarshal(value, []byte(kv.Value))
}
return &Evt{
Src: EventSrcServer,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.serversDir), "", 1),
Value: value,
}
}
func (e *EtcdStore) doWatchWithBind(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
// bind key is: bindsDir/clusterID/serverID
key := strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.bindsDir), "", 1)
infos := strings.SplitN(key, "/", 2)
return &Evt{
Src: EventSrcBind,
Type: evtType,
Key: string(kv.Key),
Value: &metapb.Bind{
ClusterID: format.MustParseStrUInt64(infos[0]),
ServerID: format.MustParseStrUInt64(infos[1]),
},
}
}
func (e *EtcdStore) doWatchWithAPI(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
value := &metapb.API{}
if len(kv.Value) > 0 {
protoc.MustUnmarshal(value, []byte(kv.Value))
}
return &Evt{
Src: EventSrcAPI,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.apisDir), "", 1),
Value: value,
}
}
func (e *EtcdStore) doWatchWithRouting(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
value := &metapb.Routing{}
if len(kv.Value) > 0 {
protoc.MustUnmarshal(value, []byte(kv.Value))
}
return &Evt{
Src: EventSrcRouting,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.routingsDir), "", 1),
Value: value,
}
}
func (e *EtcdStore) doWatchWithProxy(evtType EvtType, kv *mvccpb.KeyValue) *Evt {
value := &metapb.Proxy{}
if len(kv.Value) > 0 {
protoc.MustUnmarshal(value, []byte(kv.Value))
}
return &Evt{
Src: EventSrcProxy,
Type: evtType,
Key: strings.Replace(string(kv.Key), fmt.Sprintf("%s/", e.proxiesDir), "", 1),
Value: value,
}
}
func (e *EtcdStore) init() {
e.watchMethodMapping[EventSrcBind] = e.doWatchWithBind
e.watchMethodMapping[EventSrcServer] = e.doWatchWithServer
e.watchMethodMapping[EventSrcCluster] = e.doWatchWithCluster
e.watchMethodMapping[EventSrcAPI] = e.doWatchWithAPI
e.watchMethodMapping[EventSrcRouting] = e.doWatchWithRouting
e.watchMethodMapping[EventSrcProxy] = e.doWatchWithProxy
}