From e92a42d40e2ddd75b57db96c8126b96a8452f0da Mon Sep 17 00:00:00 2001 From: sk <123456@qq.com> Date: Fri, 24 May 2024 12:00:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0etcd=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/etcd/client.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/core/etcd/client.go b/core/etcd/client.go index e83bf65..44ee54b 100644 --- a/core/etcd/client.go +++ b/core/etcd/client.go @@ -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) } }