Move the network resolver out (#1944)

This commit is contained in:
Asim Aslam 2020-08-18 21:38:29 +01:00 committed by GitHub
parent 2eac8ed64f
commit 5d6b7b3d7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 14 additions and 64 deletions

View File

@ -7,7 +7,6 @@ import (
"io" "io"
"math" "math"
"math/rand" "math/rand"
"sort"
"sync" "sync"
"time" "time"
@ -17,9 +16,9 @@ import (
"github.com/micro/go-micro/v3/logger" "github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/network" "github.com/micro/go-micro/v3/network"
pb "github.com/micro/go-micro/v3/network/mucp/proto" 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/proxy"
"github.com/micro/go-micro/v3/registry/noop" "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/router"
"github.com/micro/go-micro/v3/server" "github.com/micro/go-micro/v3/server"
smucp "github.com/micro/go-micro/v3/server/mucp" smucp "github.com/micro/go-micro/v3/server/mucp"
@ -35,8 +34,6 @@ var (
DefaultName = "go.micro" DefaultName = "go.micro"
// DefaultAddress is default network address // DefaultAddress is default network address
DefaultAddress = ":0" 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 defines time interval to periodically announce node neighbours
AnnounceTime = 1 * time.Second AnnounceTime = 1 * time.Second
// KeepAliveTime is the time in which we want to have sent a message to a peer // 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 // resolveNodes resolves network nodes to addresses
func (n *mucpNetwork) resolveNodes() ([]string, error) { 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) nodeMap := make(map[string]bool)
// collect network node addresses // collect network node addresses
//nolint:prealloc //nolint:prealloc
var nodes []string 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 // use the DNS resolver to expand peers
dns := &dns.Resolver{} dns := &dns.Resolver{}
@ -481,6 +446,7 @@ func (n *mucpNetwork) resolveNodes() ([]string, error) {
if _, ok := nodeMap[record.Address]; !ok { if _, ok := nodeMap[record.Address]; !ok {
nodes = append(nodes, record.Address) nodes = append(nodes, record.Address)
} }
nodeMap[record.Address] = true
} }
} }
@ -1160,8 +1126,6 @@ func (n *mucpNetwork) manage() {
defer announce.Stop() defer announce.Stop()
prune := time.NewTicker(PruneTime) prune := time.NewTicker(PruneTime)
defer prune.Stop() defer prune.Stop()
resolve := time.NewTicker(ResolveTime)
defer resolve.Stop()
netsync := time.NewTicker(SyncTime) netsync := time.NewTicker(SyncTime)
defer netsync.Stop() defer netsync.Stop()
@ -1374,8 +1338,6 @@ func (n *mucpNetwork) manage() {
} }
} }
}() }()
case <-resolve.C:
n.initNodes(false)
} }
} }
} }

View File

@ -2,8 +2,6 @@ package network
import ( import (
"github.com/google/uuid" "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"
"github.com/micro/go-micro/v3/proxy/mucp" "github.com/micro/go-micro/v3/proxy/mucp"
"github.com/micro/go-micro/v3/router" "github.com/micro/go-micro/v3/router"
@ -32,8 +30,6 @@ type Options struct {
Router router.Router Router router.Router
// Proxy is network proxy // Proxy is network proxy
Proxy proxy.Proxy Proxy proxy.Proxy
// Resolver is network resolver
Resolver resolver.Resolver
} }
// Id sets the id of the network node // Id sets the id of the network node
@ -92,13 +88,6 @@ 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 // DefaultOptions returns network default options
func DefaultOptions() Options { func DefaultOptions() Options {
return Options{ return Options{
@ -108,6 +97,5 @@ func DefaultOptions() Options {
Tunnel: tmucp.NewTunnel(), Tunnel: tmucp.NewTunnel(),
Router: regRouter.NewRouter(), Router: regRouter.NewRouter(),
Proxy: mucp.NewProxy(), Proxy: mucp.NewProxy(),
Resolver: new(noop.Resolver),
} }
} }

View File

@ -5,7 +5,7 @@ import (
"context" "context"
"net" "net"
"github.com/micro/go-micro/v3/network/resolver" "github.com/micro/go-micro/v3/resolver"
"github.com/miekg/dns" "github.com/miekg/dns"
) )

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/micro/go-micro/v3/network/resolver" "github.com/micro/go-micro/v3/resolver"
) )
// Resolver is a DNS network resolve // Resolver is a DNS network resolve

View File

@ -8,7 +8,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/micro/go-micro/v3/network/resolver" "github.com/micro/go-micro/v3/resolver"
) )
// Resolver is a HTTP network resolver // Resolver is a HTTP network resolver

View File

@ -2,7 +2,7 @@
package noop package noop
import ( import (
"github.com/micro/go-micro/v3/network/resolver" "github.com/micro/go-micro/v3/resolver"
) )
type Resolver struct{} type Resolver struct{}

View File

@ -2,7 +2,7 @@
package registry package registry
import ( 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"
"github.com/micro/go-micro/v3/registry/mdns" "github.com/micro/go-micro/v3/registry/mdns"
) )

View File

@ -2,7 +2,7 @@
package static package static
import ( 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 // Resolver returns a static list of nodes. In the event the node list