From 5d6b7b3d7d3cf685ca4292dddab398fd20d82f22 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 18 Aug 2020 21:38:29 +0100 Subject: [PATCH] Move the network resolver out (#1944) --- network/mucp/mucp.go | 42 +------------------ network/options.go | 24 +++-------- {network/resolver => resolver}/dns/dns.go | 2 +- .../resolver => resolver}/dnssrv/dnssrv.go | 2 +- {network/resolver => resolver}/http/http.go | 2 +- {network/resolver => resolver}/noop/noop.go | 2 +- .../registry/registry.go | 2 +- {network/resolver => resolver}/resolver.go | 0 .../resolver => resolver}/static/static.go | 2 +- 9 files changed, 14 insertions(+), 64 deletions(-) rename {network/resolver => resolver}/dns/dns.go (96%) rename {network/resolver => resolver}/dnssrv/dnssrv.go (93%) rename {network/resolver => resolver}/http/http.go (96%) rename {network/resolver => resolver}/noop/noop.go (82%) rename {network/resolver => resolver}/registry/registry.go (94%) rename {network/resolver => resolver}/resolver.go (100%) rename {network/resolver => resolver}/static/static.go (93%) diff --git a/network/mucp/mucp.go b/network/mucp/mucp.go index 442ec957..50723ea4 100644 --- a/network/mucp/mucp.go +++ b/network/mucp/mucp.go @@ -7,7 +7,6 @@ import ( "io" "math" "math/rand" - "sort" "sync" "time" @@ -17,9 +16,9 @@ import ( "github.com/micro/go-micro/v3/logger" "github.com/micro/go-micro/v3/network" pb "github.com/micro/go-micro/v3/network/mucp/proto" - "github.com/micro/go-micro/v3/network/resolver/dns" "github.com/micro/go-micro/v3/proxy" "github.com/micro/go-micro/v3/registry/noop" + "github.com/micro/go-micro/v3/resolver/dns" "github.com/micro/go-micro/v3/router" "github.com/micro/go-micro/v3/server" smucp "github.com/micro/go-micro/v3/server/mucp" @@ -35,8 +34,6 @@ var ( DefaultName = "go.micro" // DefaultAddress is default network address DefaultAddress = ":0" - // ResolveTime defines time interval to periodically resolve network nodes - ResolveTime = 1 * time.Minute // AnnounceTime defines time interval to periodically announce node neighbours AnnounceTime = 1 * time.Second // KeepAliveTime is the time in which we want to have sent a message to a peer @@ -424,43 +421,11 @@ func (n *mucpNetwork) initNodes(startup bool) { // resolveNodes resolves network nodes to addresses func (n *mucpNetwork) resolveNodes() ([]string, error) { - // resolve the network address to network nodes - records, err := n.options.Resolver.Resolve(n.options.Name) - if err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Network failed to resolve nodes: %v", err) - } - } - - // sort by lowest priority - if err == nil && len(records) > 0 { - sort.Slice(records, func(i, j int) bool { return records[i].Priority < records[j].Priority }) - } - - // keep processing - nodeMap := make(map[string]bool) // collect network node addresses //nolint:prealloc var nodes []string - var i int - - for _, record := range records { - if _, ok := nodeMap[record.Address]; ok { - continue - } - - nodeMap[record.Address] = true - nodes = append(nodes, record.Address) - - i++ - - // break once MaxConnection nodes has been reached - if i == MaxConnections { - break - } - } // use the DNS resolver to expand peers dns := &dns.Resolver{} @@ -481,6 +446,7 @@ func (n *mucpNetwork) resolveNodes() ([]string, error) { if _, ok := nodeMap[record.Address]; !ok { nodes = append(nodes, record.Address) } + nodeMap[record.Address] = true } } @@ -1160,8 +1126,6 @@ func (n *mucpNetwork) manage() { defer announce.Stop() prune := time.NewTicker(PruneTime) defer prune.Stop() - resolve := time.NewTicker(ResolveTime) - defer resolve.Stop() netsync := time.NewTicker(SyncTime) defer netsync.Stop() @@ -1374,8 +1338,6 @@ func (n *mucpNetwork) manage() { } } }() - case <-resolve.C: - n.initNodes(false) } } } diff --git a/network/options.go b/network/options.go index 856f647b..75d49589 100644 --- a/network/options.go +++ b/network/options.go @@ -2,8 +2,6 @@ package network import ( "github.com/google/uuid" - "github.com/micro/go-micro/v3/network/resolver" - "github.com/micro/go-micro/v3/network/resolver/noop" "github.com/micro/go-micro/v3/proxy" "github.com/micro/go-micro/v3/proxy/mucp" "github.com/micro/go-micro/v3/router" @@ -32,8 +30,6 @@ type Options struct { Router router.Router // Proxy is network proxy Proxy proxy.Proxy - // Resolver is network resolver - Resolver resolver.Resolver } // Id sets the id of the network node @@ -92,22 +88,14 @@ func Proxy(p proxy.Proxy) Option { } } -// Resolver is the network resolver -func Resolver(r resolver.Resolver) Option { - return func(o *Options) { - o.Resolver = r - } -} - // DefaultOptions returns network default options func DefaultOptions() Options { return Options{ - Id: uuid.New().String(), - Name: "go.micro", - Address: ":0", - Tunnel: tmucp.NewTunnel(), - Router: regRouter.NewRouter(), - Proxy: mucp.NewProxy(), - Resolver: new(noop.Resolver), + Id: uuid.New().String(), + Name: "go.micro", + Address: ":0", + Tunnel: tmucp.NewTunnel(), + Router: regRouter.NewRouter(), + Proxy: mucp.NewProxy(), } } diff --git a/network/resolver/dns/dns.go b/resolver/dns/dns.go similarity index 96% rename from network/resolver/dns/dns.go rename to resolver/dns/dns.go index f70766a2..b85e7ec6 100644 --- a/network/resolver/dns/dns.go +++ b/resolver/dns/dns.go @@ -5,7 +5,7 @@ import ( "context" "net" - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" "github.com/miekg/dns" ) diff --git a/network/resolver/dnssrv/dnssrv.go b/resolver/dnssrv/dnssrv.go similarity index 93% rename from network/resolver/dnssrv/dnssrv.go rename to resolver/dnssrv/dnssrv.go index 667f690a..0088b422 100644 --- a/network/resolver/dnssrv/dnssrv.go +++ b/resolver/dnssrv/dnssrv.go @@ -5,7 +5,7 @@ import ( "fmt" "net" - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" ) // Resolver is a DNS network resolve diff --git a/network/resolver/http/http.go b/resolver/http/http.go similarity index 96% rename from network/resolver/http/http.go rename to resolver/http/http.go index 1ccad794..aff41f09 100644 --- a/network/resolver/http/http.go +++ b/resolver/http/http.go @@ -8,7 +8,7 @@ import ( "net/http" "net/url" - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" ) // Resolver is a HTTP network resolver diff --git a/network/resolver/noop/noop.go b/resolver/noop/noop.go similarity index 82% rename from network/resolver/noop/noop.go rename to resolver/noop/noop.go index 4be283f3..85643bf6 100644 --- a/network/resolver/noop/noop.go +++ b/resolver/noop/noop.go @@ -2,7 +2,7 @@ package noop import ( - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" ) type Resolver struct{} diff --git a/network/resolver/registry/registry.go b/resolver/registry/registry.go similarity index 94% rename from network/resolver/registry/registry.go rename to resolver/registry/registry.go index caacf431..4f54b072 100644 --- a/network/resolver/registry/registry.go +++ b/resolver/registry/registry.go @@ -2,7 +2,7 @@ package registry import ( - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" "github.com/micro/go-micro/v3/registry" "github.com/micro/go-micro/v3/registry/mdns" ) diff --git a/network/resolver/resolver.go b/resolver/resolver.go similarity index 100% rename from network/resolver/resolver.go rename to resolver/resolver.go diff --git a/network/resolver/static/static.go b/resolver/static/static.go similarity index 93% rename from network/resolver/static/static.go rename to resolver/static/static.go index a5badf1a..56d737ce 100644 --- a/network/resolver/static/static.go +++ b/resolver/static/static.go @@ -2,7 +2,7 @@ package static import ( - "github.com/micro/go-micro/v3/network/resolver" + "github.com/micro/go-micro/v3/resolver" ) // Resolver returns a static list of nodes. In the event the node list