From 4cde13c2e5c8ba6b5f42d1dfb1fa98c8bb3c9bcd Mon Sep 17 00:00:00 2001 From: vearne Date: Fri, 6 Nov 2020 11:23:34 +0800 Subject: [PATCH 1/3] add new resolver --- primitive/nsresolver.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/primitive/nsresolver.go b/primitive/nsresolver.go index d844373a..a0ab830e 100644 --- a/primitive/nsresolver.go +++ b/primitive/nsresolver.go @@ -19,6 +19,7 @@ package primitive import ( "fmt" "io/ioutil" + "net" "net/http" "os" "os/user" @@ -82,6 +83,44 @@ func (p *passthroughResolver) Description() string { return fmt.Sprintf("passthrough resolver of %v", p.addr) } +type dnsResolver struct { + addrs []string + failback NsResolver +} + +func NewDNSResolver(addrs []string) *dnsResolver { + return &dnsResolver{ + addrs: addrs, + failback: NewEnvResolver(), + } +} + +func (p *dnsResolver) Resolve() []string { + finalAddrs := make([]string, 0, len(p.addrs)) + for _, addr := range p.addrs { + // example.com:9876 + domainPort := strings.Split(addr, ":") + if len(domainPort) != 2 { + continue + } + ns, err := net.LookupHost(domainPort[0]) + if err != nil { + continue + } + for _, host := range ns { + finalAddrs = append(finalAddrs, host+":"+domainPort[1]) + } + } + if len(finalAddrs) > 0 { + return finalAddrs + } + return p.failback.Resolve() +} + +func (p *dnsResolver) Description() string { + return fmt.Sprintf("dns resolver of %v", p.addrs) +} + const ( DEFAULT_NAMESRV_ADDR = "http://jmenv.tbsite.net:8080/rocketmq/nsaddr" ) From 97c2717de9b713ea5d663706f0ca076014e6960e Mon Sep 17 00:00:00 2001 From: vearne Date: Fri, 6 Nov 2020 11:42:36 +0800 Subject: [PATCH 2/3] add unit test --- primitive/nsresolver_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/primitive/nsresolver_test.go b/primitive/nsresolver_test.go index 98d839af..52e4e09c 100644 --- a/primitive/nsresolver_test.go +++ b/primitive/nsresolver_test.go @@ -131,3 +131,21 @@ func TesHttpReslverWithSnapshotFileOnce(t *testing.T) { So(Diff(addrs1, srvs), ShouldBeFalse) }) } + +func TestDNSResolver(t *testing.T) { + Convey("Test UpdateNameServerAddress Use DNS", t, func() { + srvs := []string{ + "examples.com:9876", + } + + hosts, _ := net.LookupHost("examples.com") + expectedHosts := make([]string, 0) + for _, host:=range hosts{ + expectedHosts = append(expectedHosts, host + ":9876") + } + resolver := NewDNSResolver(srvs) + addrs := resolver.Resolve() + + So(Diff(expectedHosts, addrs), ShouldBeFalse) + }) +} \ No newline at end of file From 8208f5958d56123d3f46a0c75351fde618b03502 Mon Sep 17 00:00:00 2001 From: vearne Date: Fri, 27 Nov 2020 10:14:53 +0800 Subject: [PATCH 3/3] update primitive/nsresolver_test.go --- primitive/nsresolver_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitive/nsresolver_test.go b/primitive/nsresolver_test.go index 52e4e09c..68bb955c 100644 --- a/primitive/nsresolver_test.go +++ b/primitive/nsresolver_test.go @@ -140,12 +140,12 @@ func TestDNSResolver(t *testing.T) { hosts, _ := net.LookupHost("examples.com") expectedHosts := make([]string, 0) - for _, host:=range hosts{ - expectedHosts = append(expectedHosts, host + ":9876") + for _, host := range hosts { + expectedHosts = append(expectedHosts, host+":9876") } resolver := NewDNSResolver(srvs) addrs := resolver.Resolve() So(Diff(expectedHosts, addrs), ShouldBeFalse) }) -} \ No newline at end of file +}