From 19383fcb8be08744d9d3d50af99a3b8a9d1972b0 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Thu, 4 Jun 2015 08:31:08 +0000 Subject: [PATCH] Rename Resolver to staticResolver --- app/main.go | 2 +- app/resolver.go | 16 +++++++--------- app/resolver_test.go | 7 ++++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/main.go b/app/main.go index 49c9454c29..a6b9a8bcc5 100644 --- a/app/main.go +++ b/app/main.go @@ -67,7 +67,7 @@ func main() { c := xfer.NewCollector(*batch) defer c.Stop() - r := NewResolver(probes, c.Add) + r := newStaticResolver(probes, c.Add) defer r.Stop() lifo := NewReportLIFO(c, *window) diff --git a/app/resolver.go b/app/resolver.go index 48953fe882..dc75c3bf1d 100644 --- a/app/resolver.go +++ b/app/resolver.go @@ -15,9 +15,7 @@ var ( lookupIP = net.LookupIP ) -// Resolver periodically tries to resolve the IP addresses for a given -// set of hostnames. -type Resolver struct { +type staticResolver struct { quit chan struct{} add func(string) peers []peer @@ -33,8 +31,8 @@ type peer struct { // resolved IPs. It explictiy supports hostnames which // resolve to multiple IPs; it will repeatedly call // add with the same IP, expecting the target to dedupe. -func NewResolver(peers []string, add func(string)) Resolver { - r := Resolver{ +func newStaticResolver(peers []string, add func(string)) staticResolver { + r := staticResolver{ quit: make(chan struct{}), add: add, peers: prepareNames(peers), @@ -67,20 +65,21 @@ func prepareNames(strs []string) []peer { return results } -func (r Resolver) loop() { +func (r staticResolver) loop() { r.resolveHosts() t := tick(time.Minute) for { select { case <-t: r.resolveHosts() + case <-r.quit: return } } } -func (r Resolver) resolveHosts() { +func (r staticResolver) resolveHosts() { for _, peer := range r.peers { var addrs []net.IP if addr := net.ParseIP(peer.hostname); addr != nil { @@ -103,7 +102,6 @@ func (r Resolver) resolveHosts() { } } -// Stop this Resolver. -func (r Resolver) Stop() { +func (r staticResolver) Stop() { close(r.quit) } diff --git a/app/resolver_test.go b/app/resolver_test.go index a40d248ec9..6e47fe6b2f 100644 --- a/app/resolver_test.go +++ b/app/resolver_test.go @@ -32,17 +32,18 @@ func TestResolver(t *testing.T) { ip2 := "192.168.0.10" adds := make(chan string) add := func(s string) { adds <- s } - r := NewResolver([]string{"symbolic.name" + port, "namewithnoport", ip1 + port, ip2}, add) + + r := newStaticResolver([]string{"symbolic.name" + port, "namewithnoport", ip1 + port, ip2}, add) assertAdd := func(want string) { + _, _, line, _ := runtime.Caller(1) select { case have := <-adds: if want != have { - _, _, line, _ := runtime.Caller(1) t.Errorf("line %d: want %q, have %q", line, want, have) } case <-time.After(time.Millisecond): - t.Fatal("didn't get add in time") + t.Fatalf("line %d: didn't get add in time", line) } }