优化client初始化

This commit is contained in:
白茶清欢 2023-08-15 13:25:46 +08:00
parent d6c38ba1c0
commit aed09507e6
2 changed files with 14 additions and 5 deletions

View File

@ -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
}

10
init.go
View File

@ -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 {