增加etcd模块

This commit is contained in:
sk 2024-05-24 12:00:46 +08:00
parent 6e3b9b69e0
commit e92a42d40e
1 changed files with 6 additions and 20 deletions

View File

@ -21,6 +21,7 @@ type (
InitFunc func(ctx context.Context, res *clientv3.GetResponse) InitFunc func(ctx context.Context, res *clientv3.GetResponse)
WatchFunc func(ctx context.Context, res *clientv3.WatchResponse) WatchFunc func(ctx context.Context, res *clientv3.WatchResponse)
funcPair struct { funcPair struct {
key string
initFunc InitFunc initFunc InitFunc
watchFunc WatchFunc watchFunc WatchFunc
} }
@ -28,7 +29,7 @@ type (
type Client struct { type Client struct {
cli *clientv3.Client cli *clientv3.Client
functions map[string][]funcPair functions []funcPair
closed chan struct{} closed chan struct{}
wg sync.WaitGroup 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) logger.Logger.Infof("EtcdClient.Open(%v) success", etcdUrl)
if c.functions == nil {
c.functions = make(map[string][]funcPair)
}
c.closed = make(chan struct{}) c.closed = make(chan struct{})
return err 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) { func (c *Client) AddFunc(key string, initFunc InitFunc, watchFunc WatchFunc) {
logger.Logger.Infof("EtcdClient.AddFunc(%v)", key) logger.Logger.Infof("EtcdClient.AddFunc(%v)", key)
fs := funcPair{ fs := funcPair{
key: key,
initFunc: initFunc, initFunc: initFunc,
watchFunc: watchFunc, watchFunc: watchFunc,
} }
c.functions = append(c.functions, fs)
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
} }
// Start 重新监听 // Start 重新监听
@ -178,10 +166,8 @@ func (c *Client) Start() {
} }
logger.Logger.Infof("EtcdClient.Start") logger.Logger.Infof("EtcdClient.Start")
for k, v := range c.functions { for _, v := range c.functions {
for _, vv := range v { c.initAndWatch(v.key, v.initFunc, v.watchFunc)
c.initAndWatch(k, vv.initFunc, vv.watchFunc)
}
} }
} }