Skip to content

Commit

Permalink
Modify naming, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
logicwu0 committed Jun 25, 2024
1 parent 80a7c57 commit 06dbfbf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 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 := &EtcdConfig{
Config: &clientv3.Config{
cfg := &Config{
EtcdConfig: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Config)
etcdClient, err := clientv3.New(*cfg.EtcdConfig)
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 := &EtcdConfig{
Config: &clientv3.Config{
cfg := &Config{
EtcdConfig: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Config)
etcdClient, err := clientv3.New(*cfg.EtcdConfig)
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 := &EtcdConfig{
Config: &clientv3.Config{
cfg := &Config{
EtcdConfig: &clientv3.Config{
Endpoints: endpoints,
},
Prefix: "kitex/registry-etcd",
}
for _, opt := range opts {
opt(cfg)
}
etcdClient, err := clientv3.New(*cfg.Config)
etcdClient, err := clientv3.New(*cfg.EtcdConfig)
if err != nil {
return nil, err
}
Expand Down
26 changes: 13 additions & 13 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,36 @@ import (
)

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

type EtcdConfig struct {
Config *clientv3.Config
Prefix string
type Config struct {
EtcdConfig *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 *EtcdConfig) {
return func(cfg *Config) {
tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "")
if err != nil {
klog.Errorf("tls failed with err: %v , skipping tls.", err)
}
cfg.Config.TLS = tlsCfg
cfg.EtcdConfig.TLS = tlsCfg
}
}

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

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

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

// WithEtcdConfigAndPrefix returns an option that sets the Prefix field in the Config struct
func WithEtcdConfigAndPrefix(prefix string) Option {
return func(c *EtcdConfig) {
return func(c *Config) {
c.Prefix = prefix
}
}

0 comments on commit 06dbfbf

Please sign in to comment.