-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
129 lines (117 loc) · 2.45 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
package etcd
import (
"context"
"errors"
"github.com/leaf-rain/ctrl_c_v_golang/intermediate/recuperate"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"strings"
"sync"
)
var (
ErrClientNil = errors.New("etcd client is nil")
)
type etcdConfig struct {
ctx context.Context
client *clientv3.Client
options *options
cache *sync.Map
once *sync.Once
cancel func()
watcher *watcher
reloadChan chan string
isWatch bool
}
func NewEtcdConfig(ctx context.Context, cli *clientv3.Client, opts ...Option) (*etcdConfig, error) {
if cli == nil {
return nil, ErrClientNil
}
var cancel func()
if ctx == nil {
ctx, cancel = context.WithCancel(context.Background())
}
conf := &etcdConfig{
ctx: ctx,
client: cli,
options: NewOptions(opts...),
cache: new(sync.Map),
once: new(sync.Once),
cancel: cancel,
}
conf.watcher = newWatcher(conf)
if err := conf.Load(); err != nil {
return nil, err
}
return conf, nil
}
func (c *etcdConfig) Load() error {
for _, v := range c.options.paths {
if err := c.loadPath(v); err != nil {
return err
}
}
return nil
}
func (c *etcdConfig) Get() *sync.Map {
return c.cache
}
func (c *etcdConfig) loadPath(path string) error {
rsp, err := c.client.Get(c.ctx, c.options.root+path)
if err != nil {
return err
}
for _, item := range rsp.Kvs {
k := string(item.Key)
k = strings.ReplaceAll(k, c.options.root, "")
if k == path {
c.cache.Store(k, item.Value)
break
}
}
return nil
}
func (c *etcdConfig) getPath(str string) string {
return strings.ReplaceAll(str, c.options.root, "")
}
func (c *etcdConfig) storge(kvs []*mvccpb.KeyValue) {
for _, item := range kvs {
k := string(item.Key)
k = c.getPath(k)
for _, v := range c.options.paths {
if k == v {
c.cache.Store(k, item.Value)
if c.isWatch {
c.reloadChan <- k
}
}
}
}
}
func (c *etcdConfig) del(kvs []*mvccpb.KeyValue) {
for _, item := range kvs {
k := string(item.Key)
k = c.getPath(k)
for _, v := range c.options.paths {
if k == v {
c.cache.Delete(k)
if c.isWatch {
c.reloadChan <- k
}
}
}
}
}
func (c *etcdConfig) Watch() <-chan string {
c.once.Do(func() { // 只执行一次
c.isWatch = true // 只有当调用watch以后才开始监听
c.reloadChan = make(chan string)
go func() {
defer recuperate.Recover()
c.watcher.Watcher()
}()
})
return c.reloadChan
}
func (c *etcdConfig) Close() {
c.watcher.Close()
}