netutil: ctx-ize URLStringsEqual

Handles the case where the DNS entry will only be set up after etcd
starts.
This commit is contained in:
Anthony Romano
2016-12-14 14:07:33 -08:00
parent a9f72ee0d4
commit 13b05aeff8
2 changed files with 47 additions and 22 deletions

View File

@ -20,6 +20,9 @@ import (
"net/url" "net/url"
"reflect" "reflect"
"sort" "sort"
"time"
"golang.org/x/net/context"
"github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/pkg/types"
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"
@ -32,10 +35,12 @@ var (
resolveTCPAddr = net.ResolveTCPAddr resolveTCPAddr = net.ResolveTCPAddr
) )
const retryInterval = time.Second
// resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
// resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames // resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
// are resolved. // are resolved.
func resolveTCPAddrs(urls [][]url.URL) ([][]url.URL, error) { func resolveTCPAddrs(ctx context.Context, urls [][]url.URL) ([][]url.URL, error) {
newurls := make([][]url.URL, 0) newurls := make([][]url.URL, 0)
for _, us := range urls { for _, us := range urls {
nus := make([]url.URL, len(us)) nus := make([]url.URL, len(us))
@ -47,37 +52,52 @@ func resolveTCPAddrs(urls [][]url.URL) ([][]url.URL, error) {
nus[i] = *nu nus[i] = *nu
} }
for i, u := range nus { for i, u := range nus {
host, _, err := net.SplitHostPort(u.Host) h, err := resolveURL(ctx, u)
if err != nil { if err != nil {
plog.Errorf("could not parse url %s during tcp resolving", u.Host)
return nil, err return nil, err
} }
if host == "localhost" { if h != "" {
continue nus[i].Host = h
} }
if net.ParseIP(host) != nil {
continue
}
tcpAddr, err := resolveTCPAddr("tcp", u.Host)
if err != nil {
plog.Errorf("could not resolve host %s", u.Host)
return nil, err
}
plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
nus[i].Host = tcpAddr.String()
} }
newurls = append(newurls, nus) newurls = append(newurls, nus)
} }
return newurls, nil return newurls, nil
} }
func resolveURL(ctx context.Context, u url.URL) (string, error) {
for ctx.Err() == nil {
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
plog.Errorf("could not parse url %s during tcp resolving", u.Host)
return "", err
}
if host == "localhost" || net.ParseIP(host) != nil {
return "", nil
}
tcpAddr, err := resolveTCPAddr("tcp", u.Host)
if err == nil {
plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
return tcpAddr.String(), nil
}
plog.Warningf("failed resolving host %s (%v); retrying in %v", u.Host, err, retryInterval)
select {
case <-ctx.Done():
plog.Errorf("could not resolve host %s", u.Host)
return "", err
case <-time.After(retryInterval):
}
}
return "", ctx.Err()
}
// urlsEqual checks equality of url.URLS between two arrays. // urlsEqual checks equality of url.URLS between two arrays.
// This check pass even if an URL is in hostname and opposite is in IP address. // This check pass even if an URL is in hostname and opposite is in IP address.
func urlsEqual(a []url.URL, b []url.URL) bool { func urlsEqual(ctx context.Context, a []url.URL, b []url.URL) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false
} }
urls, err := resolveTCPAddrs([][]url.URL{a, b}) urls, err := resolveTCPAddrs(ctx, [][]url.URL{a, b})
if err != nil { if err != nil {
return false return false
} }
@ -93,7 +113,7 @@ func urlsEqual(a []url.URL, b []url.URL) bool {
return true return true
} }
func URLStringsEqual(a []string, b []string) bool { func URLStringsEqual(ctx context.Context, a []string, b []string) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false
} }
@ -114,7 +134,7 @@ func URLStringsEqual(a []string, b []string) bool {
urlsB = append(urlsB, *u) urlsB = append(urlsB, *u)
} }
return urlsEqual(urlsA, urlsB) return urlsEqual(ctx, urlsA, urlsB)
} }
func IsNetworkTimeoutError(err error) bool { func IsNetworkTimeoutError(err error) bool {

View File

@ -21,6 +21,9 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
"time"
"golang.org/x/net/context"
) )
func TestResolveTCPAddrs(t *testing.T) { func TestResolveTCPAddrs(t *testing.T) {
@ -124,7 +127,9 @@ func TestResolveTCPAddrs(t *testing.T) {
} }
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
} }
urls, err := resolveTCPAddrs(tt.urls) ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
urls, err := resolveTCPAddrs(ctx, tt.urls)
cancel()
if tt.hasError { if tt.hasError {
if err == nil { if err == nil {
t.Errorf("expected error") t.Errorf("expected error")
@ -244,14 +249,14 @@ func TestURLsEqual(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
result := urlsEqual(test.a, test.b) result := urlsEqual(context.TODO(), test.a, test.b)
if result != test.expect { if result != test.expect {
t.Errorf("a:%v b:%v, expected %v but %v", test.a, test.b, test.expect, result) t.Errorf("a:%v b:%v, expected %v but %v", test.a, test.b, test.expect, result)
} }
} }
} }
func TestURLStringsEqual(t *testing.T) { func TestURLStringsEqual(t *testing.T) {
result := URLStringsEqual([]string{"http://127.0.0.1:8080"}, []string{"http://127.0.0.1:8080"}) result := URLStringsEqual(context.TODO(), []string{"http://127.0.0.1:8080"}, []string{"http://127.0.0.1:8080"})
if !result { if !result {
t.Errorf("unexpected result %v", result) t.Errorf("unexpected result %v", result)
} }