/
registry.go
201 lines (178 loc) · 4.79 KB
/
registry.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
package service
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/aixiaoxiang/bast/conf"
"github.com/aixiaoxiang/bast/logs"
"go.etcd.io/etcd/clientv3"
)
//Registry inwrap for clientv3 of etcd
type Registry struct {
client *clientv3.Client
leaseID clientv3.LeaseID
prefix string
postfix string
stop chan struct{}
keepAliveing bool
}
//NewRegistry create an clientv3 of etcd
func NewRegistry(c *conf.RegistryConf) (*Registry, error) {
if c.DialTimeout <= 0 {
c.DialTimeout = 10
}
if c.TTL <= 0 {
c.TTL = 5
}
cli, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(c.Endpoints, ","),
DialTimeout: time.Duration(c.DialTimeout) * time.Second,
})
if err != nil {
return nil, err
}
resp, err := cli.Grant(cli.Ctx(), c.TTL)
if err != nil {
return nil, err
}
r := &Registry{
client: cli,
leaseID: resp.ID,
prefix: c.Prefix,
postfix: fmt.Sprintf(".%d", resp.ID),
}
return r, nil
}
//Put inwrap for clientv3.Put of etcd
func (r *Registry) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
if r.client == nil {
return nil, errors.New("registry etcd client is nil")
}
key = r.prefix + key + r.postfix
return r.client.Put(ctx, key, val, r.op(opts)...)
}
//Get inwrap for clientv3.Get of etcd
func (r *Registry) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
if r.client == nil {
return nil, errors.New("registry etcd client is nil")
}
key = r.prefix + key + r.postfix
return r.client.Get(ctx, key, r.op(opts)...)
}
//Delete inwrap for clientv3.Delete of etcd
func (r *Registry) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
if r.client == nil {
return nil, errors.New("registry etcd client is nil")
}
key = r.prefix + key + r.postfix
return r.client.Delete(ctx, key, r.op(opts)...)
}
//Compact inwrap for clientv3.Compact of etcd
func (r *Registry) Compact(ctx context.Context, rev int64, opts ...clientv3.CompactOption) (*clientv3.CompactResponse, error) {
if r.client == nil {
return nil, errors.New("registry etcd client is nil")
}
return r.client.Compact(ctx, rev, opts...)
}
//Do inwrap for clientv3.Do of etcd
func (r *Registry) Do(ctx context.Context, op clientv3.Op) (clientv3.OpResponse, error) {
if r.client == nil {
return clientv3.OpResponse{}, errors.New("registry etcd client is nil")
}
return r.client.Do(ctx, op)
}
//Txn inwrap for clientv3.Txn of etcd
func (r *Registry) Txn(ctx context.Context) clientv3.Txn {
if r.client == nil {
return nil
}
return r.client.Txn(ctx)
}
//Watch inwrap for clientv3.Watch of etcd
func (r *Registry) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
if r.client == nil {
return nil
}
return r.client.Watch(ctx, key, r.op(opts)...)
}
//RequestProgress inwrap for clientv3.RequestProgress of etcd
func (r *Registry) RequestProgress(ctx context.Context) error {
return r.client.RequestProgress(ctx)
}
//Close inwrap for clientv3.Close of etcd
func (r *Registry) Close() error {
return r.client.Close()
}
//Start start keepAlive and discovery
func (r *Registry) Start() error {
err := r.KeepAlive()
if err != nil {
return err
}
return nil
}
//KeepAlive start keepAlive
func (r *Registry) KeepAlive() error {
if r.keepAliveing {
return nil
}
if r.client == nil {
logs.Error("registry etcd client is nil")
return errors.New("registry etcd client is nil")
}
ch, err := r.doKeepAlive()
if err != nil {
logs.Errors("registry start", err)
return err
}
r.keepAliveing = true
for {
select {
case <-r.stop:
r.revoke()
r.keepAliveing = false
return nil
case <-r.client.Ctx().Done():
r.keepAliveing = false
logs.Error("registry keepAlive done", logs.String("server", "server closed"))
return errors.New("server closed")
case _, ok := <-ch:
if !ok {
r.keepAliveing = false
logs.Error("registry keepAlive close", logs.String("keepAlive", "keep alive channel closed"))
err := r.revoke()
return err
}
// else {
// // log.Printf("recv reply from registry: %s, ttl:%d", sv.String(), sv.TTL)
// }
}
}
}
//Stop stop keepAlive
func (r *Registry) Stop() {
if r.keepAliveing {
if r.client == nil {
return
}
r.stop <- struct{}{}
r.keepAliveing = false
}
}
func (r *Registry) op(opts []clientv3.OpOption) []clientv3.OpOption {
if opts != nil {
opts = append(opts, clientv3.WithLease(r.leaseID))
} else {
opts = []clientv3.OpOption{clientv3.WithLease(r.leaseID)}
}
return opts
}
func (r *Registry) doKeepAlive() (<-chan *clientv3.LeaseKeepAliveResponse, error) {
return r.client.KeepAlive(context.TODO(), r.leaseID)
}
func (r *Registry) revoke() error {
_, err := r.client.Revoke(context.TODO(), r.leaseID)
return err
}