优化client初始化
This commit is contained in:
parent
d6c38ba1c0
commit
aed09507e6
@ -75,3 +75,12 @@ type LeaseKeepAliveData struct {
|
|||||||
type WrapperClient struct {
|
type WrapperClient struct {
|
||||||
client *clientv3.Client
|
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
10
init.go
@ -20,14 +20,14 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Client = &client{
|
Client = &client{
|
||||||
table: make(map[string]*clientv3.Client),
|
table: make(map[string]*WrapperClient),
|
||||||
lock: &sync.RWMutex{},
|
lock: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type client struct {
|
type client struct {
|
||||||
// table 实例表
|
// table 实例表
|
||||||
table map[string]*clientv3.Client
|
table map[string]*WrapperClient
|
||||||
lock *sync.RWMutex
|
lock *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ func (c *client) AddEtcdClient(flag string, cfg clientv3.Config) error {
|
|||||||
}
|
}
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
c.table[flag] = flagClient
|
c.table[flag] = &WrapperClient{client: flagClient}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ func (c *client) AddEtcdClient(flag string, cfg clientv3.Config) error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 12:44 2023/8/15
|
// 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)
|
flagClient, _ := c.GetClientWithError(flag)
|
||||||
return flagClient
|
return flagClient
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ func (c *client) GetClient(flag string) *clientv3.Client {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 12:44 2023/8/15
|
// 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()
|
c.lock.RLock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
||||||
if _, exist := c.table[flag]; !exist {
|
if _, exist := c.table[flag]; !exist {
|
||||||
|
Loading…
Reference in New Issue
Block a user