-
Notifications
You must be signed in to change notification settings - Fork 680
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
Matt Hamann
committed
Nov 6, 2017
1 parent
2df0ea0
commit b6a0751
Showing
8 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
package resolver | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/benschw/srv-lb/lb" | ||
) | ||
|
||
// DNSConfig accepts an address and optional load balancer config | ||
type DNSConfig struct { | ||
Addr string | ||
LbCfg *lb.Config | ||
} | ||
|
||
// ResolveSrvAddr returns a load-balanced host:port based on DNS SRV lookup (or `addr` if not a hostname) | ||
func ResolveSrvAddr(dnsCfg DNSConfig) (string, error) { | ||
addr := dnsCfg.Addr | ||
|
||
if v := strings.Split(addr, ":"); len(v) < 2 { | ||
var cfg *lb.Config | ||
var err error | ||
if dnsCfg.LbCfg == nil { | ||
cfg, err = lb.DefaultConfig() | ||
|
||
if err != nil { | ||
return addr, err | ||
} | ||
} else { | ||
cfg = dnsCfg.LbCfg | ||
} | ||
|
||
l := lb.New(cfg, addr) | ||
resolvAddr, err := l.Next() | ||
|
||
if err == nil { | ||
addr = resolvAddr.String() | ||
} | ||
} | ||
|
||
return addr, nil | ||
} |
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,29 @@ | ||
package resolver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/benschw/srv-lb/lb" | ||
) | ||
|
||
func TestSrvLookup(t *testing.T) { | ||
lbCfg, _ := lb.DefaultConfig() | ||
lbCfg.Strategy = MockStrategy | ||
dnsCfg := DNSConfig{Addr: "foo.example.com", LbCfg: lbCfg} | ||
|
||
addr, _ := ResolveSrvAddr(dnsCfg) | ||
|
||
if addr != "1.2.3.4:1234" { | ||
t.Error("expected address string of 1.2.3.4:1234, got:", addr) | ||
} | ||
} | ||
|
||
func TestIpPassthrough(t *testing.T) { | ||
dnsCfg := DNSConfig{Addr: "10.0.0.1:1234"} | ||
|
||
addr, _ := ResolveSrvAddr(dnsCfg) | ||
|
||
if addr != "10.0.0.1:1234" { | ||
t.Error("expected output to equal input (10.0.0.1:1234), got:", addr) | ||
} | ||
} |
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,30 @@ | ||
package resolver | ||
|
||
import ( | ||
"github.com/benschw/srv-lb/dns" | ||
"github.com/benschw/srv-lb/lb" | ||
) | ||
|
||
// MockStrategy is used for testing DNS load balancing | ||
const MockStrategy lb.StrategyType = "mock" | ||
|
||
// New creates a new instance of the load balancer | ||
func New(lib dns.Lookup) lb.GenericLoadBalancer { | ||
lb := new(MockClb) | ||
lb.dnsLib = lib | ||
return lb | ||
} | ||
|
||
// MockClb contains the dnslib | ||
type MockClb struct { | ||
dnsLib dns.Lookup | ||
} | ||
|
||
// Next gets the next server in the available nodes | ||
func (lb *MockClb) Next(name string) (dns.Address, error) { | ||
return dns.Address{Address: "1.2.3.4", Port: 1234}, nil | ||
} | ||
|
||
func init() { | ||
lb.RegisterStrategy(MockStrategy, New) | ||
} |
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