Skip to content

Commit

Permalink
Add domain name resolution for gossip address seed
Browse files Browse the repository at this point in the history
  • Loading branch information
kevburnsjr committed Jan 6, 2025
1 parent f78448e commit 429d908
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"net"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -449,6 +450,27 @@ func (a *Agent) stopReplica(cfg ReplicaConfig) (err error) {
return
}

func (a *Agent) gossipIP(peerApiAddr string) (ipAddr string, err error) {
parts := strings.Split(peerApiAddr, ":")
if len(parts) != 2 {
err = fmt.Errorf("%w: %s", ErrInvalidGossipAddr, peerApiAddr)
return
}
if net.ParseIP(parts[0]) != nil {
return peerApiAddr, nil
}
ips, err := net.LookupIP(parts[0])
if err != nil {
return
}
if len(ips) == 0 {
err = fmt.Errorf("%w: %s", ErrInvalidGossipAddr, peerApiAddr)
} else {
ipAddr = ips[0].String() + ":" + parts[1]
}
return
}

// resolvePeerGossipSeed resolves peer api address list to peer gossip address list
func (a *Agent) resolvePeerGossipSeed() (gossip []string, err error) {
a.wg.Add(1)
Expand All @@ -457,15 +479,25 @@ func (a *Agent) resolvePeerGossipSeed() (gossip []string, err error) {
gossip = gossip[:0]
for _, peerApiAddr := range a.peers {
if peerApiAddr == a.advertiseAddress {
gossip = append(gossip, a.hostConfig.Gossip.AdvertiseAddress)
ipAddr, err := a.gossipIP(a.hostConfig.Gossip.AdvertiseAddress)
if err == nil {
gossip = append(gossip, ipAddr)
} else {
a.log.Warningf(err.Error())
}
continue
}
var res *internal.ProbeResponse
ctx, cancel := context.WithTimeout(context.Background(), raftTimeout)
defer cancel()
res, err = a.grpcClientPool.get(peerApiAddr).Probe(ctx, &internal.ProbeRequest{})
if err == nil && res != nil {
gossip = append(gossip, res.GossipAdvertiseAddress)
ipAddr, err := a.gossipIP(res.GossipAdvertiseAddress)
if err == nil {
gossip = append(gossip, ipAddr)
} else {
a.log.Warningf(err.Error())
}
} else if err != nil && !strings.HasSuffix(err.Error(), `connect: connection refused"`) {
a.log.Warningf("No probe response for %s %+v %v", peerApiAddr, res, err.Error())
}
Expand Down
1 change: 1 addition & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var (
ErrHostNotFound = fmt.Errorf(`Host not found`)
ErrIDOutOfRange = fmt.Errorf(`ID out of range`)
ErrInvalidFactory = fmt.Errorf(`Invalid Factory`)
ErrInvalidGossipAddr = fmt.Errorf(`Invalid gossip address`)
ErrReplicaNotActive = fmt.Errorf("Replica not active")
ErrReplicaNotAllowed = fmt.Errorf("Replica not allowed")
ErrReplicaNotFound = fmt.Errorf("Replica not found")
Expand Down

0 comments on commit 429d908

Please sign in to comment.