/
kv_client_handler.go
76 lines (66 loc) · 2.11 KB
/
kv_client_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package kvfactory
import (
"fmt"
"sync"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/milvus-io/milvus/pkg/util/etcd"
"github.com/milvus-io/milvus/pkg/util/paramtable"
)
var clientCreator = &etcdClientCreator{}
var getEtcdAndPathFunction = getEtcdAndPath
type etcdClientCreator struct {
mu sync.Mutex
client *clientv3.Client
rootpath *string
}
// Returns an Etcd client and the metaRootPath, if an error is hit, will panic.
// This client is reused by all calls in the current runtime.
func GetEtcdAndPath() (*clientv3.Client, string) {
client, path := getEtcdAndPathFunction()
return client, path
}
// Reset the stored client, mainly used during testing when paramtable params have changed
// during runtime.
func CloseEtcdClient() {
clientCreator.mu.Lock()
defer clientCreator.mu.Unlock()
if clientCreator.client != nil {
err := clientCreator.client.Close()
if err != nil {
panic(err)
}
}
clientCreator.client = nil
clientCreator.rootpath = nil
}
// Returns an Etcd client and the metaRootPath, if an error is hit, will panic
func getEtcdAndPath() (*clientv3.Client, string) {
clientCreator.mu.Lock()
defer clientCreator.mu.Unlock()
// If client/path doesn’t exist, create a new one
if clientCreator.client == nil {
var err error
clientCreator.client, err = createEtcdClient()
if err != nil {
panic(fmt.Errorf("failed to create etcd client: %w", err))
}
path := paramtable.Get().ServiceParam.EtcdCfg.MetaRootPath.GetValue()
clientCreator.rootpath = &path
}
return clientCreator.client, *clientCreator.rootpath
}
// Function that calls the Etcd constructor
func createEtcdClient() (*clientv3.Client, error) {
cfg := ¶mtable.Get().ServiceParam
return etcd.CreateEtcdClient(
cfg.EtcdCfg.UseEmbedEtcd.GetAsBool(),
cfg.EtcdCfg.EtcdEnableAuth.GetAsBool(),
cfg.EtcdCfg.EtcdAuthUserName.GetValue(),
cfg.EtcdCfg.EtcdAuthPassword.GetValue(),
cfg.EtcdCfg.EtcdUseSSL.GetAsBool(),
cfg.EtcdCfg.Endpoints.GetAsStrings(),
cfg.EtcdCfg.EtcdTLSCert.GetValue(),
cfg.EtcdCfg.EtcdTLSKey.GetValue(),
cfg.EtcdCfg.EtcdTLSCACert.GetValue(),
cfg.EtcdCfg.EtcdTLSMinVersion.GetValue())
}