forked from kudoochui/kudos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
257 lines (256 loc) · 7.25 KB
/
etcd.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package serverplugin
//
//func init() {
// etcd.Register()
//}
//
//// EtcdRegisterPlugin implements etcd registry.
//type EtcdRegisterPlugin struct {
// // service address, for example, tcp@127.0.0.1:8972, quic@127.0.0.1:1234
// ServiceAddress string
// // etcd addresses
// EtcdServers []string
// // base path for rpcx server, for example com/example/rpcx
// BasePath string
// Metrics metrics.Registry
// // Registered services
// Services []string
// servicesLock sync.RWMutex
// metasLock sync.RWMutex
// metas map[string]string
// UpdateInterval time.Duration
//
// Options *store.Config
// kv store.Store
//
// dying chan struct{}
// done chan struct{}
//}
//
//// Start starts to connect etcd cluster
//func (p *EtcdRegisterPlugin) Start() error {
// if p.done == nil {
// p.done = make(chan struct{})
// }
// if p.dying == nil {
// p.dying = make(chan struct{})
// }
//
// if p.kv == nil {
// kv, err := libkv.NewStore(store.ETCD, p.EtcdServers, p.Options)
// if err != nil {
// log.Errorf("cannot create etcd registry: %v", err)
// return err
// }
// p.kv = kv
// }
//
// err := p.kv.Put(p.BasePath, []byte("rpcx_path"), &store.WriteOptions{IsDir: true})
// if err != nil && !strings.Contains(err.Error(), "Not a file") {
// log.Errorf("cannot create etcd path %s: %v", p.BasePath, err)
// return err
// }
//
// if p.UpdateInterval > 0 {
// ticker := time.NewTicker(p.UpdateInterval)
// go func() {
// defer p.kv.Close()
//
// // refresh service TTL
// for {
// select {
// case <-p.dying:
// close(p.done)
// return
// case <-ticker.C:
// extra := make(map[string]string)
// if p.Metrics != nil {
// extra["calls"] = fmt.Sprintf("%.2f", metrics.GetOrRegisterMeter("calls", p.Metrics).RateMean())
// extra["connections"] = fmt.Sprintf("%.2f", metrics.GetOrRegisterMeter("connections", p.Metrics).RateMean())
// }
// //set this same metrics for all services at this server
// p.servicesLock.RLock()
// for _, name := range p.Services {
// nodePath := fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
// kvPair, err := p.kv.Get(nodePath)
// if err != nil {
// log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error())
//
// p.metasLock.RLock()
// meta := p.metas[name]
// p.metasLock.RUnlock()
//
// err = p.kv.Put(nodePath, []byte(meta), &store.WriteOptions{TTL: p.UpdateInterval * 3})
// if err != nil {
// log.Errorf("cannot re-create etcd path %s: %v", nodePath, err)
// }
//
// } else {
// v, _ := url.ParseQuery(string(kvPair.Value))
// for key, value := range extra {
// v.Set(key, value)
// }
// p.kv.Put(nodePath, []byte(v.Encode()), &store.WriteOptions{TTL: p.UpdateInterval * 3})
// }
// }
// p.servicesLock.RUnlock()
// }
// }
// }()
// }
//
// return nil
//}
//
//// Stop unregister all services.
//func (p *EtcdRegisterPlugin) Stop() error {
// if p.kv == nil {
// kv, err := libkv.NewStore(store.ETCD, p.EtcdServers, p.Options)
// if err != nil {
// log.Errorf("cannot create etcd registry: %v", err)
// return err
// }
// p.kv = kv
// }
//
// for _, name := range p.Services {
// nodePath := fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
// exist, err := p.kv.Exists(nodePath)
// if err != nil {
// log.Errorf("cannot delete path %s: %v", nodePath, err)
// continue
// }
// if exist {
// p.kv.Delete(nodePath)
// log.Infof("delete path %s", nodePath, err)
// }
// }
//
// close(p.dying)
// <-p.done
// return nil
//}
//
//// HandleConnAccept handles connections from clients
//func (p *EtcdRegisterPlugin) HandleConnAccept(conn net.Conn) (net.Conn, bool) {
// if p.Metrics != nil {
// metrics.GetOrRegisterMeter("connections", p.Metrics).Mark(1)
// }
// return conn, true
//}
//
//// PreCall handles rpc call from clients
//func (p *EtcdRegisterPlugin) PreCall(_ context.Context, _, _ string, args interface{}) (interface{}, error) {
// if p.Metrics != nil {
// metrics.GetOrRegisterMeter("calls", p.Metrics).Mark(1)
// }
// return args, nil
//}
//
//// Register handles registering event.
//// this service is registered at BASE/serviceName/thisIpAddress node
//func (p *EtcdRegisterPlugin) Register(name string, rcvr interface{}, metadata string) (err error) {
// if strings.TrimSpace(name) == "" {
// err = errors.New("Register service `name` can't be empty")
// return
// }
//
// if p.kv == nil {
// etcd.Register()
// kv, err := libkv.NewStore(store.ETCD, p.EtcdServers, nil)
// if err != nil {
// log.Errorf("cannot create etcd registry: %v", err)
// return err
// }
// p.kv = kv
// }
//
// err = p.kv.Put(p.BasePath, []byte("rpcx_path"), &store.WriteOptions{IsDir: true})
// if err != nil && !strings.Contains(err.Error(), "Not a file") {
// log.Errorf("cannot create etcd path %s: %v", p.BasePath, err)
// return err
// }
//
// nodePath := fmt.Sprintf("%s/%s", p.BasePath, name)
// err = p.kv.Put(nodePath, []byte(name), &store.WriteOptions{IsDir: true})
// if err != nil && !strings.Contains(err.Error(), "Not a file") {
// log.Errorf("cannot create etcd path %s: %v", nodePath, err)
// return err
// }
//
// nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
// err = p.kv.Put(nodePath, []byte(metadata), &store.WriteOptions{TTL: p.UpdateInterval * 2})
// if err != nil {
// log.Errorf("cannot create etcd path %s: %v", nodePath, err)
// return err
// }
// p.servicesLock.Lock()
// p.Services = append(p.Services, name)
// p.servicesLock.Unlock()
//
// p.metasLock.Lock()
// if p.metas == nil {
// p.metas = make(map[string]string)
// }
// p.metas[name] = metadata
// p.metasLock.Unlock()
// return
//}
//
//func (p *EtcdRegisterPlugin) RegisterFunction(serviceName, fname string, fn interface{}, metadata string) error {
// return p.Register(serviceName, fn, metadata)
//}
//
//func (p *EtcdRegisterPlugin) Unregister(name string) (err error) {
// if strings.TrimSpace(name) == "" {
// err = errors.New("Register service `name` can't be empty")
// return
// }
//
// if p.kv == nil {
// etcd.Register()
// kv, err := libkv.NewStore(store.ETCD, p.EtcdServers, nil)
// if err != nil {
// log.Errorf("cannot create etcd registry: %v", err)
// return err
// }
// p.kv = kv
// }
//
// err = p.kv.Put(p.BasePath, []byte("rpcx_path"), &store.WriteOptions{IsDir: true})
// if err != nil && !strings.Contains(err.Error(), "Not a file") {
// log.Errorf("cannot create etcd path %s: %v", p.BasePath, err)
// return err
// }
//
// nodePath := fmt.Sprintf("%s/%s", p.BasePath, name)
// err = p.kv.Put(nodePath, []byte(name), &store.WriteOptions{IsDir: true})
// if err != nil && !strings.Contains(err.Error(), "Not a file") {
// log.Errorf("cannot create etcd path %s: %v", nodePath, err)
// return err
// }
//
// nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
//
// err = p.kv.Delete(nodePath)
// if err != nil {
// log.Errorf("cannot create consul path %s: %v", nodePath, err)
// return err
// }
//
// var services = make([]string, 0, len(p.Services)-1)
// for _, s := range p.Services {
// if s != name {
// services = append(services, s)
// }
// }
// p.Services = services
//
// p.metasLock.Lock()
// if p.metas == nil {
// p.metas = make(map[string]string)
// }
// delete(p.metas, name)
// p.metasLock.Unlock()
// return
//}