Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow specifying the address to register #32

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading