Files
wehub-resource-sync 498b235461
Build and test / Build and test AMD64 Ubuntu 22.04 (push) Failing after 0s
Publish Builder / amazonlinux2023 (push) Failing after 1s
Build and test / UT for Go (push) Has been skipped
Publish KRTE Images / KRTE (push) Failing after 1s
Build and test / Integration Test (push) Has been skipped
Build and test / Upload Code Coverage (push) Has been skipped
Publish Builder / rockylinux9 (push) Failing after 1s
Publish Builder / ubuntu22.04 (push) Failing after 0s
Publish Builder / ubuntu24.04 (push) Failing after 0s
Publish Gpu Builder / publish-gpu-builder (push) Failing after 1s
Publish Test Images / PyTest (push) Failing after 0s
Build and test / UT for Cpp (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:17 +08:00

79 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package kvfactory
import (
"fmt"
"sync"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/milvus-io/milvus/pkg/v3/util/etcd"
"github.com/milvus-io/milvus/pkg/v3/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 doesnt 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().EtcdCfg.MetaRootPath.GetValue()
clientCreator.rootpath = &path
}
return clientCreator.client, *clientCreator.rootpath
}
// Function that calls the Etcd constructor
func createEtcdClient() (*clientv3.Client, error) {
cfg := &paramtable.Get().ServiceParam
options := cfg.EtcdCfg.ClientOptions()
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(),
options...)
}