-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcddriver.go
220 lines (190 loc) · 4.68 KB
/
etcddriver.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
package etcddriver
import (
"context"
"log"
"sync"
"time"
"github.com/dcron-contrib/commons"
"github.com/dcron-contrib/commons/dlog"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)
const (
etcdDefaultLease = 5 // min lease time
etcdDialTimeout = 3 * time.Second
etcdBusinessTimeout = 5 * time.Second
)
type EtcdDriver struct {
nodeID string
serviceName string
cli *clientv3.Client
nodes *sync.Map
logger dlog.Logger
lease int64
leaseID clientv3.LeaseID
leaseCh <-chan *clientv3.LeaseKeepAliveResponse
ctx context.Context
cancel context.CancelFunc
}
// NewEtcdDriver
func NewDriver(cli *clientv3.Client) *EtcdDriver {
ser := &EtcdDriver{
cli: cli,
nodes: &sync.Map{},
logger: &dlog.StdLogger{
Log: log.Default(),
},
}
return ser
}
// 设置key value,绑定租约
func (e *EtcdDriver) putKeyWithLease(ctx context.Context, key, val string) (clientv3.LeaseID, error) {
//设置租约时间,最少5s
if e.lease < etcdDefaultLease {
e.lease = etcdDefaultLease
}
subCtx, cancel := context.WithTimeout(ctx, etcdBusinessTimeout)
defer cancel()
resp, err := e.cli.Grant(subCtx, e.lease)
if err != nil {
return 0, err
}
//注册服务并绑定租约
_, err = e.cli.Put(subCtx, key, val, clientv3.WithLease(resp.ID))
if err != nil {
return 0, err
}
return resp.ID, nil
}
// WatchService 初始化服务列表和监视
func (e *EtcdDriver) watchService(ctx context.Context, serviceName string) error {
prefix := commons.GetKeyPre(serviceName)
// 根据前缀获取现有的key
resp, err := e.cli.Get(ctx, prefix, clientv3.WithPrefix())
if err != nil {
return err
}
for _, ev := range resp.Kvs {
e.setServiceList(string(ev.Key), string(ev.Value))
}
// 监视前缀,修改变更的server
go e.watcher(serviceName)
return nil
}
// watcher 监听前缀
func (e *EtcdDriver) watcher(serviceName string) {
prefix := commons.GetKeyPre(serviceName)
rch := e.cli.Watch(context.Background(), prefix, clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
switch ev.Type {
case mvccpb.PUT:
// 修改或者新增
e.setServiceList(string(ev.Kv.Key), string(ev.Kv.Value))
case mvccpb.DELETE:
// 删除
e.delServiceList(string(ev.Kv.Key))
}
}
}
}
// setServiceList 新增服务地址
func (e *EtcdDriver) setServiceList(key, val string) {
e.nodes.Store(key, val)
}
// DelServiceList 删除服务地址
func (e *EtcdDriver) delServiceList(key string) {
e.nodes.Delete(key)
}
// GetServices 获取服务地址
func (e *EtcdDriver) getServices() []string {
addrs := make([]string, 0)
e.nodes.Range(func(key, _ interface{}) bool {
addrs = append(addrs, key.(string))
return true
})
return addrs
}
func (e *EtcdDriver) keepAlive(ctx context.Context, nodeID string) (<-chan *clientv3.LeaseKeepAliveResponse, error) {
var err error
e.leaseID, err = e.putKeyWithLease(ctx, nodeID, nodeID)
if err != nil {
e.logger.Errorf("putKeyWithLease error: %v", err)
return nil, err
}
return e.cli.KeepAlive(ctx, e.leaseID)
}
func (e *EtcdDriver) revoke(ctx context.Context) {
_, err := e.cli.Lease.Revoke(ctx, e.leaseID)
if err != nil {
e.logger.Infof("lease revoke error: %v", err)
}
}
func (e *EtcdDriver) startHeartBeat(ctx context.Context) {
var err error
e.leaseCh, err = e.keepAlive(ctx, e.nodeID)
if err != nil {
e.logger.Errorf("keep alive error, %v", err)
return
}
}
func (e *EtcdDriver) keepHeartBeat() {
for {
select {
case <-e.ctx.Done():
{
e.logger.Warnf("driver stopped")
return
}
case _, ok := <-e.leaseCh:
{
if !ok {
e.logger.Warnf("lease channel stop, driver stopped")
return
}
}
}
}
}
func (e *EtcdDriver) Init(serverName string, opts ...commons.Option) {
e.serviceName = serverName
e.nodeID = commons.GetNodeId(serverName)
for _, opt := range opts {
e.WithOption(opt)
}
}
func (e *EtcdDriver) NodeID() string {
return e.nodeID
}
func (e *EtcdDriver) GetNodes(ctx context.Context) (nodes []string, err error) {
return e.getServices(), nil
}
func (e *EtcdDriver) Start(ctx context.Context) (err error) {
// renew a global ctx when start every time
e.ctx, e.cancel = context.WithCancel(context.TODO())
e.startHeartBeat(ctx)
err = e.watchService(ctx, e.serviceName)
if err != nil {
return
}
go e.keepHeartBeat()
return
}
func (e *EtcdDriver) Stop(ctx context.Context) (err error) {
e.revoke(ctx)
e.cancel()
return
}
func (e *EtcdDriver) WithOption(opt commons.Option) (err error) {
switch opt.Type() {
case commons.OptionTypeTimeout:
{
e.lease = int64(opt.(commons.TimeoutOption).Timeout.Seconds())
}
case commons.OptionTypeLogger:
{
e.logger = opt.(commons.LoggerOption).Logger
}
}
return
}