Skip to content

Commit

Permalink
netbalancer: more consistent handling of empty host list
Browse files Browse the repository at this point in the history
  • Loading branch information
esiqveland committed Apr 9, 2019
1 parent 91b5a71 commit 2ea2d75
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
16 changes: 8 additions & 8 deletions netbalancer/netbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
// We can not use TTL from dns because TTL is not exposed by the Go calls.
func New(host string, port int, updateInterval, timeout time.Duration) (balancer.Balancer, error) {
initialHosts, err := lookupTimeout(timeout, host, port)
if err != nil {
return nil, err
}
if len(initialHosts) == 0 {
return nil, errors.Wrapf(err, "Error no ips found for host=%v", host)
return nil, balancer.ErrNoHosts
}

bal := &dnsBalancer{
Expand Down Expand Up @@ -74,7 +77,7 @@ func (b *dnsBalancer) update() {
// TODO: set hostList to empty?
// TODO: log?
} else {
if len(nextHostList) > 0 {
if nextHostList != nil {
log.Printf("[DnsBalancer] reloaded dns=%v hosts=%v", b.lookupAddress, nextHostList)
b.lock.Lock()
b.hosts = nextHostList
Expand All @@ -97,16 +100,13 @@ func lookupTimeout(timeout time.Duration, host string, port int) ([]balancer.Hos
}

func lookup(ctx context.Context, host string, port int) ([]balancer.Host, error) {
hosts := []balancer.Host{}

ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, errors.Wrapf(err, "Error looking up initial list for host=%v", host)
return hosts, errors.Wrapf(err, "Error looking up host=%v", host)
}

if len(ips) == 0 {
return nil, balancer.ErrNoHosts
}

hosts := []balancer.Host{}
for k := range ips {
entry := balancer.Host{
Address: ips[k].IP,
Expand Down
12 changes: 5 additions & 7 deletions netbalancer/srvbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/esiqveland/balancer"
"github.com/pkg/errors"
)

type dnsSrvBalancer struct {
Expand All @@ -24,16 +23,15 @@ type dnsSrvBalancer struct {
Timeout time.Duration
}

// NewNetBalancer returns a Balancer that uses dns lookups from net.Lookup* to reload a set of hosts every updateInterval.
// We can not use TTL from dns because TTL is not exposed by the Go calls.
// NewSRV returns a Balancer that uses dns lookups from net.LookupSRV to reload a set of hosts every updateInterval.
// We can not use TTL from dns because TTL is not exposed by the Go stdlib.
func NewSRV(servicename, proto, host string, updateInterval time.Duration, dnsTimeout time.Duration) (balancer.Balancer, error) {
initialHosts, err := lookupSRVTimeout(dnsTimeout, servicename, proto, host)
if err != nil {
return nil, err
}

if len(initialHosts) == 0 {
return nil, errors.Wrapf(err, "Error no ips found for host=%v", host)
return nil, balancer.ErrNoHosts
}

bal := &dnsSrvBalancer{
Expand Down Expand Up @@ -119,8 +117,8 @@ func (b *dnsSrvBalancer) update() {
// TODO: set hostList to empty?
// TODO: log?
} else {
if len(nextHostList) > 0 {
log.Printf("[DnsBalancer] reloaded dns=%v hosts=%v", b.host, nextHostList)
if nextHostList != nil {
log.Printf("[SRVBalancer] reloaded dns=%v hosts=%v", b.host, nextHostList)
b.lock.Lock()
b.hosts = nextHostList
b.lock.Unlock()
Expand Down

0 comments on commit 2ea2d75

Please sign in to comment.