Skip to content

Commit

Permalink
Support modification of prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
logicwu0 committed Jun 23, 2024
1 parent cad3c1e commit 80a7c57
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
12 changes: 6 additions & 6 deletions etcd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ type registerMeta struct {

// NewEtcdRegistry creates an etcd based registry.
func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, error) {
cfg := &ConfigWithPrefix{
Configs: &clientv3.Config{
cfg := &EtcdConfig{
Config: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Configs)
etcdClient, err := clientv3.New(*cfg.Config)
if err != nil {
return nil, err
}
Expand All @@ -91,16 +91,16 @@ func SetFixedAddress(r registry.Registry, address net.Addr) {

// NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs
func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) {
cfg := &ConfigWithPrefix{
Configs: &clientv3.Config{
cfg := &EtcdConfig{
Config: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Configs)
etcdClient, err := clientv3.New(*cfg.Config)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions etcd_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ type etcdResolver struct {

// NewEtcdResolver creates a etcd based resolver.
func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, error) {
cfg := &ConfigWithPrefix{
Configs: &clientv3.Config{
cfg := &EtcdConfig{
Config: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Configs)
etcdClient, err := clientv3.New(*cfg.Config)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion etcd_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"io/ioutil" //nolint
"math/big"
"net"
"net/url"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/kitex-contrib/registry-etcd

go 1.21

//toolchain go1.21.4
toolchain go1.21.4

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

Expand Down
32 changes: 16 additions & 16 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,42 @@ import (
"errors"
"github.com/cloudwego/kitex/pkg/klog"
clientv3 "go.etcd.io/etcd/client/v3"
"io/ioutil"
"io/ioutil" //nolint
"time"
)

// Option sets options such as username, tls etc.
type Option func(cfg *ConfigWithPrefix)
type Option func(cfg *EtcdConfig)

type EtcdConfig struct {
Config *clientv3.Config
Prefix string
}
type EtcdOption func(cfg *EtcdConfig)

// WithTLSOpt returns a option that authentication by tls/ssl.
func WithTLSOpt(certFile, keyFile, caFile string) Option {
return func(cfg *ConfigWithPrefix) {
return func(cfg *EtcdConfig) {
tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "")
if err != nil {
klog.Errorf("tls failed with err: %v , skipping tls.", err)
}
cfg.Configs.TLS = tlsCfg
cfg.Config.TLS = tlsCfg
}
}

// WithAuthOpt returns a option that authentication by usernane and password.
func WithAuthOpt(username, password string) Option {
return func(cfg *ConfigWithPrefix) {
cfg.Configs.Username = username
cfg.Configs.Password = password
return func(cfg *EtcdConfig) {
cfg.Config.Username = username
cfg.Config.Password = password
}
}

// WithDialTimeoutOpt returns a option set dialTimeout
func WithDialTimeoutOpt(dialTimeout time.Duration) Option {
return func(cfg *ConfigWithPrefix) {
cfg.Configs.DialTimeout = dialTimeout
return func(cfg *EtcdConfig) {
cfg.Config.DialTimeout = dialTimeout
}
}

Expand All @@ -74,14 +80,8 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er
return cfg, nil
}

type ConfigWithPrefix struct {
Configs *clientv3.Config
Prefix string
}
type EtcdOption func(cfg *ConfigWithPrefix)

func WithEtcdConfigAndPrefix(prefix string) Option {
return func(c *ConfigWithPrefix) {
return func(c *EtcdConfig) {
c.Prefix = prefix
}
}

0 comments on commit 80a7c57

Please sign in to comment.