增加etcd模块
This commit is contained in:
parent
6e3b9b69e0
commit
e92a42d40e
|
@ -21,6 +21,7 @@ type (
|
|||
InitFunc func(ctx context.Context, res *clientv3.GetResponse)
|
||||
WatchFunc func(ctx context.Context, res *clientv3.WatchResponse)
|
||||
funcPair struct {
|
||||
key string
|
||||
initFunc InitFunc
|
||||
watchFunc WatchFunc
|
||||
}
|
||||
|
@ -28,7 +29,7 @@ type (
|
|||
|
||||
type Client struct {
|
||||
cli *clientv3.Client
|
||||
functions map[string][]funcPair
|
||||
functions []funcPair
|
||||
closed chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
@ -66,10 +67,6 @@ func (c *Client) Open(etcdUrl []string, userName, passWord string, dialTimeout t
|
|||
|
||||
logger.Logger.Infof("EtcdClient.Open(%v) success", etcdUrl)
|
||||
|
||||
if c.functions == nil {
|
||||
c.functions = make(map[string][]funcPair)
|
||||
}
|
||||
|
||||
c.closed = make(chan struct{})
|
||||
return err
|
||||
}
|
||||
|
@ -153,20 +150,11 @@ func (c *Client) WatchWithPrefix(prefix string, revision int64) clientv3.WatchCh
|
|||
func (c *Client) AddFunc(key string, initFunc InitFunc, watchFunc WatchFunc) {
|
||||
logger.Logger.Infof("EtcdClient.AddFunc(%v)", key)
|
||||
fs := funcPair{
|
||||
key: key,
|
||||
initFunc: initFunc,
|
||||
watchFunc: watchFunc,
|
||||
}
|
||||
|
||||
if c.functions == nil {
|
||||
c.functions = make(map[string][]funcPair)
|
||||
}
|
||||
|
||||
a, ok := c.functions[key]
|
||||
if !ok {
|
||||
a = []funcPair{}
|
||||
}
|
||||
a = append(a, fs)
|
||||
c.functions[key] = a
|
||||
c.functions = append(c.functions, fs)
|
||||
}
|
||||
|
||||
// Start 重新监听
|
||||
|
@ -178,10 +166,8 @@ func (c *Client) Start() {
|
|||
}
|
||||
|
||||
logger.Logger.Infof("EtcdClient.Start")
|
||||
for k, v := range c.functions {
|
||||
for _, vv := range v {
|
||||
c.initAndWatch(k, vv.initFunc, vv.watchFunc)
|
||||
}
|
||||
for _, v := range c.functions {
|
||||
c.initAndWatch(v.key, v.initFunc, v.watchFunc)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue