Skip to content

Commit

Permalink
Merge pull request #32 from kitex-contrib/feat/allow-specify-address
Browse files Browse the repository at this point in the history
feat: allow specifying the address to register
  • Loading branch information
Felix021 authored Mar 1, 2024
2 parents e697f9f + 3e20e80 commit 326b822
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion etcd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type etcdRegistry struct {
meta *registerMeta
retryConfig *retry.Config
stop chan struct{}
address net.Addr
}

type registerMeta struct {
Expand Down Expand Up @@ -73,6 +74,16 @@ func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, err
}, nil
}

// SetFixedAddress sets the fixed address for registering
// setting address to nil to clear the previous address
func SetFixedAddress(r registry.Registry, address net.Addr) {
if er, ok := r.(*etcdRegistry); ok {
er.address = address
return
}
panic("invalid registry type: not etcdRegistry")
}

// NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs
func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) {
cfg := clientv3.Config{
Expand Down Expand Up @@ -154,8 +165,13 @@ func (e *etcdRegistry) register(info *registry.Info, leaseID clientv3.LeaseID) e
if err != nil {
return err
}
network := info.Addr.Network()
if e.address != nil {
network = e.address.Network()
addr = e.address.String()
}
val, err := json.Marshal(&instanceInfo{
Network: info.Addr.Network(),
Network: network,
Address: addr,
Weight: info.Weight,
Tags: info.Tags,
Expand Down

0 comments on commit 326b822

Please sign in to comment.