etcd: extract NewEtcdClient into exported function

In order for us to be able to re-use the code for initializing an etcd
client connection, we extract and export the NewEtcdClient function.
This commit is contained in:
Oliver Gugger committed 2022-10-13 09:37:52 +02:00
1 parent 98f359c5b7
commit d576184fdb
2 files changed
+45 -6

No files matched your search

+17 -5
View File
@@ -133,9 +133,10 @@ type db struct {
// Enforce db implements the walletdb.DB interface.
var _ walletdb.DB = (*db)(nil)
// newEtcdBackend returns a db object initialized with the passed backend
// config. If etcd connection cannot be established, then returns error.
func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
// NewEtcdClient creates a new etcd v3 API client.
func NewEtcdClient(ctx context.Context, cfg Config) (*clientv3.Client,
context.Context, func(), error) {
clientCfg := clientv3.Config{
Endpoints: []string{cfg.Host},
DialTimeout: etcdConnectionTimeout,
@@ -153,7 +154,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, err
return nil, nil, nil, err
}
clientCfg.TLS = tlsConfig
@@ -164,7 +165,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
cli, err := clientv3.New(clientCfg)
if err != nil {
cancel()
return nil, err
return nil, nil, nil, err
}
// Apply the namespace.
@@ -172,6 +173,17 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
cli.Watcher = namespace.NewWatcher(cli.Watcher, cfg.Namespace)
cli.Lease = namespace.NewLease(cli.Lease, cfg.Namespace)
return cli, ctx, cancel, nil
}
// newEtcdBackend returns a db object initialized with the passed backend
// config. If etcd connection cannot be established, then returns error.
func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
cli, ctx, cancel, err := NewEtcdClient(ctx, cfg)
if err != nil {
return nil, err
}
backend := &db{
cfg: cfg,
ctx: ctx,