-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dec12c
commit 1fd6bce
Showing
3 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package netbalancer | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/esiqveland/balancer" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type dnsSrvBalancer struct { | ||
serviceName string | ||
proto string | ||
host string | ||
hosts []balancer.Host | ||
counter uint64 | ||
interval time.Duration | ||
quit chan int | ||
lock *sync.Mutex | ||
} | ||
|
||
// 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. | ||
func NewSRV(servicename, proto, host string, updateInterval time.Duration) (balancer.Balancer, error) { | ||
initialHosts, err := lookupSRV(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) | ||
} | ||
|
||
bal := &dnsSrvBalancer{ | ||
serviceName: servicename, | ||
proto: proto, | ||
host: host, | ||
hosts: initialHosts, | ||
interval: updateInterval, | ||
counter: 0, | ||
quit: make(chan int, 1), | ||
lock: &sync.Mutex{}, | ||
} | ||
|
||
// start update loop | ||
go bal.update() | ||
|
||
return bal, nil | ||
} | ||
|
||
func lookupSRV(servicename, proto, host string) ([]balancer.Host, error) { | ||
hosts := []balancer.Host{} | ||
|
||
_, addrs, err := net.LookupSRV(servicename, proto, host) | ||
if err != nil { | ||
return hosts, err | ||
} | ||
|
||
var firstErr error = nil | ||
|
||
for _, v := range addrs { | ||
ips, err := net.DefaultResolver.LookupIPAddr(context.TODO(), v.Target) | ||
if err != nil { | ||
if firstErr == nil { | ||
firstErr = err | ||
} | ||
continue | ||
} | ||
|
||
for e := range ips { | ||
host := balancer.Host{ | ||
Address: ips[e].IP, | ||
Port: int(v.Port), | ||
} | ||
hosts = append(hosts, host) | ||
} | ||
} | ||
|
||
return hosts, firstErr | ||
} | ||
|
||
func (b *dnsSrvBalancer) Next() (balancer.Host, error) { | ||
// make sure to store a reference before we start | ||
hosts := b.hosts | ||
count := uint64(len(hosts)) | ||
if count == 0 { | ||
return balancer.Host{}, balancer.ErrNoHosts | ||
} | ||
|
||
nextNum := atomic.AddUint64(&b.counter, 1) | ||
|
||
idx := nextNum % count | ||
|
||
return hosts[idx], nil | ||
} | ||
|
||
func (b *dnsSrvBalancer) update() { | ||
tick := time.NewTicker(b.interval) | ||
|
||
for { | ||
select { | ||
case <-tick.C: | ||
// TODO: timeout | ||
// TODO: retries? | ||
nextHostList, err := lookupSRV(b.serviceName, b.proto, b.host) | ||
if err != nil { | ||
// TODO: set hostList to empty? | ||
// TODO: log? | ||
} else { | ||
if len(nextHostList) > 0 { | ||
log.Printf("[DnsBalancer] reloaded dns=%v hosts=%v", b.host, nextHostList) | ||
b.lock.Lock() | ||
b.hosts = nextHostList | ||
b.lock.Unlock() | ||
} | ||
} | ||
case <-b.quit: | ||
tick.Stop() | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (b *dnsSrvBalancer) Close() error { | ||
// TODO: wait for exit | ||
b.quit <- 1 | ||
|
||
return nil | ||
} |