Skip to content

Commit

Permalink
Merge pull request #309 from devopsfaith/weighted_dns
Browse files Browse the repository at this point in the history
Weighted hostnames
  • Loading branch information
kpacha committed Dec 13, 2019
2 parents 60c2359 + bd41ee2 commit d756c46
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 6 additions & 3 deletions sd/dnssrv/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ func (s subscriber) resolve() ([]string, error) {
if err != nil {
return []string{}, err
}
instances := make([]string, len(addrs))
for i, addr := range addrs {
instances[i] = fmt.Sprintf("http://%s", net.JoinHostPort(addr.Target, fmt.Sprint(addr.Port)))
instances := []string{}
for _, addr := range addrs {
instances = append(instances, fmt.Sprintf("http://%s", net.JoinHostPort(addr.Target, fmt.Sprint(addr.Port))))
for i := 0; i < int(addr.Weight-1); i++ {
instances = append(instances, fmt.Sprintf("http://%s", net.JoinHostPort(addr.Target, fmt.Sprint(addr.Port))))
}
}
return instances, nil
}
7 changes: 6 additions & 1 deletion sd/dnssrv/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ func TestSubscriber_New(t *testing.T) {
{
Port: 80,
Target: "127.0.0.1",
Weight: 1,
},
{
Port: 81,
Target: "127.0.0.1",
Weight: 2,
},
}
DefaultLookup = func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
Expand All @@ -33,7 +35,7 @@ func TestSubscriber_New(t *testing.T) {
if err != nil {
t.Error("Getting the hosts:", err.Error())
}
if len(hosts) != 2 {
if len(hosts) != 3 {
t.Error("Wrong number of hosts:", len(hosts))
}
if hosts[0] != "http://127.0.0.1:80" {
Expand All @@ -42,6 +44,9 @@ func TestSubscriber_New(t *testing.T) {
if hosts[1] != "http://127.0.0.1:81" {
t.Error("Wrong host #1 (expected http://127.0.0.1:81):", hosts[1])
}
if hosts[2] != "http://127.0.0.1:81" {
t.Error("Wrong host #2 (expected http://127.0.0.1:81):", hosts[2])
}
}

func TestSubscriber_LoockupError(t *testing.T) {
Expand Down

0 comments on commit d756c46

Please sign in to comment.