From aed09507e69e9651df4bbc5f0196b26a7ce1d9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 15 Aug 2023 13:25:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96client=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 9 +++++++++ init.go | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/define.go b/define.go index a8b232e..d977336 100644 --- a/define.go +++ b/define.go @@ -75,3 +75,12 @@ type LeaseKeepAliveData struct { type WrapperClient struct { client *clientv3.Client } + +// GetClient 获取客户端连接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:25 2023/8/15 +func (wc *WrapperClient) GetClient() *clientv3.Client { + return wc.client +} diff --git a/init.go b/init.go index a06c80a..52b6a5b 100644 --- a/init.go +++ b/init.go @@ -20,14 +20,14 @@ var ( func init() { Client = &client{ - table: make(map[string]*clientv3.Client), + table: make(map[string]*WrapperClient), lock: &sync.RWMutex{}, } } type client struct { // table 实例表 - table map[string]*clientv3.Client + table map[string]*WrapperClient lock *sync.RWMutex } @@ -74,7 +74,7 @@ func (c *client) AddEtcdClient(flag string, cfg clientv3.Config) error { } c.lock.Lock() defer c.lock.Unlock() - c.table[flag] = flagClient + c.table[flag] = &WrapperClient{client: flagClient} return nil } @@ -83,7 +83,7 @@ func (c *client) AddEtcdClient(flag string, cfg clientv3.Config) error { // Author : go_developer@163.com<白茶清欢> // // Date : 12:44 2023/8/15 -func (c *client) GetClient(flag string) *clientv3.Client { +func (c *client) GetClient(flag string) *WrapperClient { flagClient, _ := c.GetClientWithError(flag) return flagClient } @@ -93,7 +93,7 @@ func (c *client) GetClient(flag string) *clientv3.Client { // Author : go_developer@163.com<白茶清欢> // // Date : 12:44 2023/8/15 -func (c *client) GetClientWithError(flag string) (*clientv3.Client, error) { +func (c *client) GetClientWithError(flag string) (*WrapperClient, error) { c.lock.RLock() defer c.lock.RUnlock() if _, exist := c.table[flag]; !exist {