mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Updated comments. Tiny cleanup changes. (#1093)
This commit is contained in:
parent
048065fe96
commit
59fccb82ec
@ -6,7 +6,6 @@ import (
|
|||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -54,16 +53,16 @@ type network struct {
|
|||||||
options Options
|
options Options
|
||||||
// rtr is network router
|
// rtr is network router
|
||||||
router router.Router
|
router router.Router
|
||||||
// prx is network proxy
|
// proxy is network proxy
|
||||||
proxy proxy.Proxy
|
proxy proxy.Proxy
|
||||||
// tun is network tunnel
|
// tunnel is network tunnel
|
||||||
tunnel tunnel.Tunnel
|
tunnel tunnel.Tunnel
|
||||||
// server is network server
|
// server is network server
|
||||||
server server.Server
|
server server.Server
|
||||||
// client is network client
|
// client is network client
|
||||||
client client.Client
|
client client.Client
|
||||||
|
|
||||||
// tunClient is a map of tunnel clients keyed over tunnel channel names
|
// tunClient is a map of tunnel channel clients
|
||||||
tunClient map[string]transport.Client
|
tunClient map[string]transport.Client
|
||||||
// peerLinks is a map of links for each peer
|
// peerLinks is a map of links for each peer
|
||||||
peerLinks map[string]tunnel.Link
|
peerLinks map[string]tunnel.Link
|
||||||
@ -89,9 +88,9 @@ type message struct {
|
|||||||
|
|
||||||
// newNetwork returns a new network node
|
// newNetwork returns a new network node
|
||||||
func newNetwork(opts ...Option) Network {
|
func newNetwork(opts ...Option) Network {
|
||||||
rand.Seed(time.Now().UnixNano())
|
// create default options
|
||||||
options := DefaultOptions()
|
options := DefaultOptions()
|
||||||
|
// initialize network options
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
@ -180,11 +179,12 @@ func newNetwork(opts ...Option) Network {
|
|||||||
|
|
||||||
func (n *network) Init(opts ...Option) error {
|
func (n *network) Init(opts ...Option) error {
|
||||||
n.Lock()
|
n.Lock()
|
||||||
|
defer n.Unlock()
|
||||||
|
|
||||||
// TODO: maybe only allow reinit of certain opts
|
// TODO: maybe only allow reinit of certain opts
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&n.options)
|
o(&n.options)
|
||||||
}
|
}
|
||||||
n.Unlock()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -192,14 +192,21 @@ func (n *network) Init(opts ...Option) error {
|
|||||||
// Options returns network options
|
// Options returns network options
|
||||||
func (n *network) Options() Options {
|
func (n *network) Options() Options {
|
||||||
n.RLock()
|
n.RLock()
|
||||||
|
defer n.RUnlock()
|
||||||
|
|
||||||
options := n.options
|
options := n.options
|
||||||
n.RUnlock()
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns network name
|
// Name returns network name
|
||||||
func (n *network) Name() string {
|
func (n *network) Name() string {
|
||||||
return n.options.Name
|
n.RLock()
|
||||||
|
defer n.RUnlock()
|
||||||
|
|
||||||
|
name := n.options.Name
|
||||||
|
|
||||||
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
// acceptNetConn accepts connections from NetworkChannel
|
// acceptNetConn accepts connections from NetworkChannel
|
||||||
@ -341,8 +348,11 @@ func (n *network) advertise(advertChan <-chan *router.Advert) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initNodes initializes tunnel with a list of resolved nodes
|
||||||
func (n *network) initNodes(startup bool) {
|
func (n *network) initNodes(startup bool) {
|
||||||
nodes, err := n.resolveNodes()
|
nodes, err := n.resolveNodes()
|
||||||
|
// NOTE: this condition never fires
|
||||||
|
// as resolveNodes() never returns error
|
||||||
if err != nil && !startup {
|
if err != nil && !startup {
|
||||||
log.Debugf("Network failed to resolve nodes: %v", err)
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
||||||
return
|
return
|
||||||
@ -394,7 +404,7 @@ func (n *network) resolveNodes() ([]string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use the dns resolver to expand peers
|
// use the DNS resolver to expand peers
|
||||||
dns := &dns.Resolver{}
|
dns := &dns.Resolver{}
|
||||||
|
|
||||||
// append seed nodes if we have them
|
// append seed nodes if we have them
|
||||||
@ -964,7 +974,7 @@ func (n *network) prunePeerRoutes(peer *node) error {
|
|||||||
|
|
||||||
// manage the process of announcing to peers and prune any peer nodes that have not been
|
// manage the process of announcing to peers and prune any peer nodes that have not been
|
||||||
// seen for a period of time. Also removes all the routes either originated by or routable
|
// seen for a period of time. Also removes all the routes either originated by or routable
|
||||||
//by the stale nodes. it also resolves nodes periodically and adds them to the tunnel
|
// by the stale nodes. it also resolves nodes periodically and adds them to the tunnel
|
||||||
func (n *network) manage() {
|
func (n *network) manage() {
|
||||||
announce := time.NewTicker(AnnounceTime)
|
announce := time.NewTicker(AnnounceTime)
|
||||||
defer announce.Stop()
|
defer announce.Stop()
|
||||||
@ -1046,7 +1056,6 @@ func (n *network) manage() {
|
|||||||
|
|
||||||
// we're only going to send to max 3 peers at any given tick
|
// we're only going to send to max 3 peers at any given tick
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
|
|
||||||
// advertise yourself to the network
|
// advertise yourself to the network
|
||||||
if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
|
if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
|
||||||
log.Debugf("Network failed to advertise peer %s: %v", peer.id, err)
|
log.Debugf("Network failed to advertise peer %s: %v", peer.id, err)
|
||||||
@ -1060,7 +1069,7 @@ func (n *network) manage() {
|
|||||||
// now look at links we may not have sent to. this may occur
|
// now look at links we may not have sent to. this may occur
|
||||||
// where a connect message was lost
|
// where a connect message was lost
|
||||||
for link, lastSent := range links {
|
for link, lastSent := range links {
|
||||||
if !lastSent.IsZero() {
|
if !lastSent.IsZero() || time.Since(lastSent) < KeepAliveTime {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1111,11 +1120,11 @@ func (n *network) manage() {
|
|||||||
// mark as processed
|
// mark as processed
|
||||||
routers[route.Router] = true
|
routers[route.Router] = true
|
||||||
|
|
||||||
// if the router is NOT in our peer graph, delete all routes originated by it
|
// if the router is in our peer graph do NOT delete routes originated by it
|
||||||
if peer := n.node.GetPeerNode(route.Router); peer != nil {
|
if peer := n.node.GetPeerNode(route.Router); peer != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// otherwise delete all the routes originated by it
|
||||||
if err := n.pruneRoutes(router.QueryRouter(route.Router)); err != nil {
|
if err := n.pruneRoutes(router.QueryRouter(route.Router)); err != nil {
|
||||||
log.Debugf("Network failed deleting routes by %s: %v", route.Router, err)
|
log.Debugf("Network failed deleting routes by %s: %v", route.Router, err)
|
||||||
}
|
}
|
||||||
@ -1233,31 +1242,33 @@ func (n *network) updatePeerLinks(peer *node) error {
|
|||||||
// if the peerLink is found in the returned links update peerLinks
|
// if the peerLink is found in the returned links update peerLinks
|
||||||
log.Tracef("Network updating peer links for peer %s", peer.address)
|
log.Tracef("Network updating peer links for peer %s", peer.address)
|
||||||
|
|
||||||
// add peerLink to the peerLinks map
|
// lookup a link and update it if better link is available
|
||||||
if link, ok := n.peerLinks[peer.address]; ok {
|
if link, ok := n.peerLinks[peer.address]; ok {
|
||||||
// if the existing has better Length then the new, replace it
|
// if the existing has better Length then the new, replace it
|
||||||
if link.Length() < peerLink.Length() {
|
if link.Length() < peerLink.Length() {
|
||||||
n.peerLinks[peer.address] = peerLink
|
n.peerLinks[peer.address] = peerLink
|
||||||
}
|
}
|
||||||
} else {
|
return nil
|
||||||
n.peerLinks[peer.address] = peerLink
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add peerLink to the peerLinks map
|
||||||
|
n.peerLinks[peer.address] = peerLink
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isLoopback checks if a link is a loopback to ourselves
|
// isLoopback checks if a link is a loopback to ourselves
|
||||||
func (n *network) isLoopback(link tunnel.Link) bool {
|
func (n *network) isLoopback(link tunnel.Link) bool {
|
||||||
// our advertise address
|
|
||||||
loopback := n.server.Options().Advertise
|
|
||||||
// actual address
|
|
||||||
address := n.tunnel.Address()
|
|
||||||
|
|
||||||
// skip loopback
|
// skip loopback
|
||||||
if link.Loopback() {
|
if link.Loopback() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// our advertise address
|
||||||
|
loopback := n.server.Options().Advertise
|
||||||
|
// actual address
|
||||||
|
address := n.tunnel.Address()
|
||||||
|
|
||||||
// if remote is ourselves
|
// if remote is ourselves
|
||||||
switch link.Remote() {
|
switch link.Remote() {
|
||||||
case loopback, address:
|
case loopback, address:
|
||||||
@ -1382,7 +1393,10 @@ func (n *network) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dial into ControlChannel to send route adverts
|
// dial into ControlChannel to send route adverts
|
||||||
ctrlClient, err := n.tunnel.Dial(ControlChannel, tunnel.DialMode(tunnel.Multicast))
|
ctrlClient, err := n.tunnel.Dial(
|
||||||
|
ControlChannel,
|
||||||
|
tunnel.DialMode(tunnel.Multicast),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1390,7 +1404,10 @@ func (n *network) Connect() error {
|
|||||||
n.tunClient[ControlChannel] = ctrlClient
|
n.tunClient[ControlChannel] = ctrlClient
|
||||||
|
|
||||||
// dial into NetworkChannel to send network messages
|
// dial into NetworkChannel to send network messages
|
||||||
netClient, err := n.tunnel.Dial(NetworkChannel, tunnel.DialMode(tunnel.Multicast))
|
netClient, err := n.tunnel.Dial(
|
||||||
|
NetworkChannel,
|
||||||
|
tunnel.DialMode(tunnel.Multicast),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user