2019-09-10 00:01:41 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-10 01:14:23 +01:00
|
|
|
"container/list"
|
2019-09-10 00:01:41 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-09-10 18:32:25 +01:00
|
|
|
pb "github.com/micro/go-micro/network/proto"
|
2019-09-10 00:01:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// MaxDepth defines max depth of peer topology
|
2019-09-10 18:32:25 +01:00
|
|
|
MaxDepth uint = 3
|
2019-09-10 00:01:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// node is network node
|
|
|
|
type node struct {
|
|
|
|
sync.RWMutex
|
|
|
|
// id is node id
|
|
|
|
id string
|
|
|
|
// address is node address
|
|
|
|
address string
|
2019-09-10 01:14:23 +01:00
|
|
|
// peers are nodes with direct link to this node
|
|
|
|
peers map[string]*node
|
2019-09-10 00:01:41 +01:00
|
|
|
// network returns the node network
|
|
|
|
network Network
|
2019-09-10 01:14:23 +01:00
|
|
|
// lastSeen keeps track of node lifetime and updates
|
2019-09-10 00:01:41 +01:00
|
|
|
lastSeen time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id is node ide
|
|
|
|
func (n *node) Id() string {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns node address
|
|
|
|
func (n *node) Address() string {
|
|
|
|
return n.address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns node network
|
|
|
|
func (n *node) Network() Network {
|
|
|
|
return n.network
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
// AddPeer adds a new peer to node topology
|
2019-09-13 03:02:20 +01:00
|
|
|
// It returns false if the peer already exists
|
|
|
|
func (n *node) AddPeer(peer *node) bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if _, ok := n.peers[peer.id]; !ok {
|
|
|
|
n.peers[peer.id] = peer
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePeer updates a peer if it exists
|
|
|
|
// It returns false if the peer does not exist
|
|
|
|
func (n *node) UpdatePeer(peer *node) bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if _, ok := n.peers[peer.id]; ok {
|
|
|
|
n.peers[peer.id] = peer
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePeer deletes a peer if it exists
|
|
|
|
func (n *node) DeletePeer(id string) {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
delete(n.peers, id)
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
// HasPeer returns true if node has peer with given id
|
|
|
|
func (n *node) HasPeer(id string) bool {
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
_, ok := n.peers[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshPeer updates node timestamp
|
|
|
|
// It returns false if the peer has not been found.
|
2019-09-13 03:02:20 +01:00
|
|
|
func (n *node) RefreshPeer(id string, now time.Time) bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
peer, ok := n.peers[id]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.lastSeen.Before(now) {
|
|
|
|
peer.lastSeen = now
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
func (n *node) walk(until func(peer *node) bool) map[string]*node {
|
2019-09-11 23:03:27 +01:00
|
|
|
// track the visited nodes
|
2019-09-10 13:31:02 +01:00
|
|
|
visited := make(map[string]*node)
|
|
|
|
// queue of the nodes to visit
|
|
|
|
queue := list.New()
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
// push node to the back of queue
|
|
|
|
queue.PushBack(n)
|
|
|
|
// mark the node as visited
|
|
|
|
visited[n.id] = n
|
|
|
|
|
|
|
|
// keep iterating over the queue until its empty
|
|
|
|
for queue.Len() > 0 {
|
|
|
|
// pop the node from the front of the queue
|
|
|
|
qnode := queue.Front()
|
|
|
|
// iterate through all of the node peers
|
|
|
|
// mark the visited nodes; enqueue the non-visted
|
|
|
|
for id, node := range qnode.Value.(*node).peers {
|
|
|
|
if _, ok := visited[id]; !ok {
|
|
|
|
visited[id] = node
|
|
|
|
queue.PushBack(node)
|
|
|
|
}
|
2019-09-13 18:46:24 +01:00
|
|
|
if until(node) {
|
|
|
|
return visited
|
|
|
|
}
|
2019-09-10 01:14:23 +01:00
|
|
|
}
|
|
|
|
// remove the node from the queue
|
|
|
|
queue.Remove(qnode)
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
return visited
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes returns a slice if all nodes in node topology
|
|
|
|
func (n *node) Nodes() []Node {
|
|
|
|
// we need to freeze the network graph here
|
|
|
|
// otherwise we might get inconsisten results
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
// NOTE: this should never be true
|
|
|
|
untilNoMorePeers := func(n *node) bool {
|
|
|
|
return n == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
visited := n.walk(untilNoMorePeers)
|
|
|
|
|
2019-09-10 00:01:41 +01:00
|
|
|
var nodes []Node
|
2019-09-10 01:14:23 +01:00
|
|
|
// collect all the nodes and return them
|
|
|
|
for _, node := range visited {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
// GetPeerNode returns a peer from node topology i.e. up to MaxDepth
|
|
|
|
// It returns nil if the peer was not found in the node topology
|
|
|
|
func (n *node) GetPeerNode(id string) *node {
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
// get node topology up to MaxDepth
|
|
|
|
top := n.Topology(MaxDepth)
|
|
|
|
|
|
|
|
untilFoundPeer := func(n *node) bool {
|
|
|
|
return n.id == id
|
|
|
|
}
|
|
|
|
|
|
|
|
visited := top.walk(untilFoundPeer)
|
|
|
|
|
|
|
|
peerNode, ok := visited[id]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Topology returns a copy of th node topology down to given depth
|
|
|
|
func (n *node) Topology(depth uint) *node {
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-09-11 00:23:37 +01:00
|
|
|
// make a copy of yourself
|
|
|
|
node := &node{
|
2019-09-13 03:02:20 +01:00
|
|
|
id: n.id,
|
|
|
|
address: n.address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
network: n.network,
|
|
|
|
lastSeen: n.lastSeen,
|
2019-09-11 00:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// return if we reach requested depth or we have no more peers
|
|
|
|
if depth == 0 || len(n.peers) == 0 {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2019-09-11 23:03:27 +01:00
|
|
|
// decrement the depth
|
2019-09-11 00:23:37 +01:00
|
|
|
depth--
|
|
|
|
|
2019-09-11 23:03:27 +01:00
|
|
|
// iterate through our peers and update the node peers
|
2019-09-11 00:23:37 +01:00
|
|
|
for _, peer := range n.peers {
|
2019-09-13 18:46:24 +01:00
|
|
|
nodePeer := peer.Topology(depth)
|
2019-09-11 23:03:27 +01:00
|
|
|
if _, ok := node.peers[nodePeer.id]; !ok {
|
|
|
|
node.peers[nodePeer.id] = nodePeer
|
|
|
|
}
|
2019-09-10 18:32:25 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:23:37 +01:00
|
|
|
return node
|
2019-09-10 18:32:25 +01:00
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
// Peers returns node peers up to MaxDepth
|
2019-09-11 23:03:27 +01:00
|
|
|
func (n *node) Peers() []Node {
|
|
|
|
n.RLock()
|
2019-09-13 18:46:24 +01:00
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-09-11 23:03:27 +01:00
|
|
|
var peers []Node
|
|
|
|
for _, nodePeer := range n.peers {
|
2019-09-13 18:46:24 +01:00
|
|
|
peer := nodePeer.Topology(MaxDepth)
|
2019-09-11 23:03:27 +01:00
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2019-09-13 03:02:20 +01:00
|
|
|
// UnpackPeerTopology unpacks pb.Peer into node topology of given depth
|
|
|
|
func UnpackPeerTopology(pbPeer *pb.Peer, lastSeen time.Time, depth uint) *node {
|
2019-09-10 01:14:23 +01:00
|
|
|
peerNode := &node{
|
2019-09-13 03:02:20 +01:00
|
|
|
id: pbPeer.Node.Id,
|
|
|
|
address: pbPeer.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
lastSeen: lastSeen,
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
// return if have either reached the depth or have no more peers
|
|
|
|
if depth == 0 || len(pbPeer.Peers) == 0 {
|
|
|
|
return peerNode
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrement the depth
|
|
|
|
depth--
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
peers := make(map[string]*node)
|
|
|
|
for _, pbPeer := range pbPeer.Peers {
|
2019-09-13 03:02:20 +01:00
|
|
|
peer := UnpackPeerTopology(pbPeer, lastSeen, depth)
|
2019-09-10 01:14:23 +01:00
|
|
|
peers[pbPeer.Node.Id] = peer
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
peerNode.peers = peers
|
2019-09-10 00:01:41 +01:00
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
return peerNode
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
func peerProtoTopology(peer Node, depth uint) *pb.Peer {
|
2019-09-11 23:56:57 +01:00
|
|
|
node := &pb.Node{
|
|
|
|
Id: peer.Id(),
|
|
|
|
Address: peer.Address(),
|
|
|
|
}
|
2019-09-11 18:56:28 +01:00
|
|
|
|
2019-09-11 23:56:57 +01:00
|
|
|
pbPeers := &pb.Peer{
|
|
|
|
Node: node,
|
|
|
|
Peers: make([]*pb.Peer, 0),
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 23:56:57 +01:00
|
|
|
// return if we reached the end of topology or depth
|
|
|
|
if depth == 0 || len(peer.Peers()) == 0 {
|
|
|
|
return pbPeers
|
|
|
|
}
|
2019-09-10 01:14:23 +01:00
|
|
|
|
2019-09-11 23:56:57 +01:00
|
|
|
// decrement the depth
|
|
|
|
depth--
|
2019-09-10 00:01:41 +01:00
|
|
|
|
2019-09-11 23:56:57 +01:00
|
|
|
// iterate through peers of peers aka pops
|
|
|
|
for _, pop := range peer.Peers() {
|
2019-09-13 18:46:24 +01:00
|
|
|
peer := peerProtoTopology(pop, depth)
|
2019-09-11 23:56:57 +01:00
|
|
|
pbPeers.Peers = append(pbPeers.Peers, peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbPeers
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeersToProto returns node peers graph encoded into protobuf
|
2019-09-13 03:02:20 +01:00
|
|
|
func PeersToProto(node Node, depth uint) *pb.Peer {
|
2019-09-11 23:56:57 +01:00
|
|
|
// network node aka root node
|
2019-09-13 03:02:20 +01:00
|
|
|
pbNode := &pb.Node{
|
|
|
|
Id: node.Id(),
|
|
|
|
Address: node.Address(),
|
2019-09-11 23:56:57 +01:00
|
|
|
}
|
|
|
|
// we will build proto topology into this
|
|
|
|
pbPeers := &pb.Peer{
|
2019-09-13 03:02:20 +01:00
|
|
|
Node: pbNode,
|
2019-09-11 23:56:57 +01:00
|
|
|
Peers: make([]*pb.Peer, 0),
|
|
|
|
}
|
|
|
|
|
2019-09-13 03:02:20 +01:00
|
|
|
for _, peer := range node.Peers() {
|
2019-09-13 18:46:24 +01:00
|
|
|
pbPeer := peerProtoTopology(peer, depth)
|
2019-09-11 23:56:57 +01:00
|
|
|
pbPeers.Peers = append(pbPeers.Peers, pbPeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbPeers
|
2019-09-10 00:01:41 +01:00
|
|
|
}
|