-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd.go
210 lines (174 loc) · 4.55 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
package etcd
import (
"context"
"time"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/mvcc/mvccpb"
"go.etcd.io/etcd/clientv3"
)
const (
EtcdEventTypeUpdate = "update"
EtcdEventTypeCreate = "create"
EtcdEventTypeDelete = "delete"
)
const etcdOpTimeout = 5 * time.Second
type EtcdStorage struct {
id string
watchPrefix string
client *clientv3.Client
exitChan chan struct{}
watchCtx context.Context
watchCancel context.CancelFunc
}
func NewEtcdStorage(id, watchPrefix string, client *clientv3.Client) *EtcdStorage {
etcd := &EtcdStorage{
id: id,
watchPrefix: watchPrefix,
client: client,
exitChan: make(chan struct{}),
}
return etcd
}
func (etcd *EtcdStorage) WatchPrefix() string {
return etcd.watchPrefix
}
func (etcd *EtcdStorage) SetWatchPrefix(watchPrefix string) {
etcd.watchPrefix = watchPrefix
}
func (etcd *EtcdStorage) Close() {
close(etcd.exitChan)
etcd.client.Close()
}
func (etcd *EtcdStorage) WatchClose() {
if etcd.watchCancel != nil {
etcd.watchCancel()
}
}
func (etcd *EtcdStorage) Query(key string, withprefix bool, endkey string, limit int64, revision *int64) (*clientv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), etcdOpTimeout)
defer cancel()
opts := []clientv3.OpOption{}
if withprefix {
opts = []clientv3.OpOption{
clientv3.WithRange(endkey),
clientv3.WithKeysOnly(),
clientv3.WithSerializable(),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
}
if limit > 0 {
opts = append(opts, clientv3.WithLimit(limit))
}
}
if revision != nil {
if *revision >= 0 {
opts = append(opts, clientv3.WithRev(*revision))
}
}
return etcd.client.Get(ctx, key, opts...)
}
func (etcd *EtcdStorage) Create(key, val string, lease int64) (*clientv3.PutResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), etcdOpTimeout)
defer cancel()
opts := []clientv3.OpOption{
clientv3.WithPrevKV(),
}
if lease > 0 {
lease, err := etcd.client.Grant(ctx, lease)
if err != nil {
return nil, err
}
opts = append(opts, clientv3.WithLease(lease.ID))
}
return etcd.client.Put(ctx, key, val, opts...)
}
func (etcd *EtcdStorage) Delete(key string, withprefix bool) error {
ctx, cancel := context.WithTimeout(context.Background(), etcdOpTimeout)
defer cancel()
opts := []clientv3.OpOption{}
if withprefix {
opts = append(opts, clientv3.WithPrefix())
}
_, err := etcd.client.Delete(ctx, key, opts...)
return err
}
func (etcd *EtcdStorage) GetRevision(key string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), etcdOpTimeout)
defer cancel()
opts := []clientv3.OpOption{
clientv3.WithCountOnly(),
}
if len(key) == 0 {
opts = append(opts, clientv3.WithFromKey())
key = "\x00"
}
resp, err := etcd.client.Get(ctx, key, opts...)
if err != nil {
return 0, err
}
return resp.Header.GetRevision() + 1, nil
}
type EtcdEvent struct {
ID string `json:"-"`
Type string `json:"type"`
More bool `json:"more"`
Header *pb.ResponseHeader `json:"header"`
Kvs []*mvccpb.KeyValue `json:"kvs"`
PrevKv *mvccpb.KeyValue `json:"prev_kv,omitempty"`
}
func (etcd *EtcdStorage) Watch(revision int64, key string, c chan *EtcdEvent) {
opts := []clientv3.OpOption{
clientv3.WithPrefix(),
clientv3.WithPrevKV(),
clientv3.WithRev(revision),
}
if len(key) == 0 {
opts = append(opts, clientv3.WithFromKey())
key = "\x00"
}
for {
etcd.watchCtx, etcd.watchCancel = context.WithCancel(context.Background())
watchC := etcd.client.Watch(etcd.watchCtx, key, opts...)
loop:
for {
select {
case <-etcd.exitChan:
etcd.watchCancel()
return
case resp, ok := <-watchC:
if resp.Canceled || !ok || resp.Err() != nil {
etcd.watchCancel()
time.Sleep(100 * time.Millisecond)
v, err := etcd.GetRevision(key)
if err == nil {
revision = v
}
break loop
}
for _, event := range resp.Events {
switch event.Type {
case clientv3.EventTypeDelete:
c <- &EtcdEvent{
ID: etcd.id,
Type: EtcdEventTypeDelete,
Header: &resp.Header,
Kvs: []*mvccpb.KeyValue{event.Kv},
PrevKv: event.PrevKv,
}
case clientv3.EventTypePut:
typ := EtcdEventTypeUpdate
if event.IsCreate() {
typ = EtcdEventTypeCreate
}
c <- &EtcdEvent{
ID: etcd.id,
Type: typ,
Header: &resp.Header,
Kvs: []*mvccpb.KeyValue{event.Kv},
PrevKv: event.PrevKv,
}
}
}
}
}
}
}