Skip to content

Commit

Permalink
Merge pull request micro#867 from milosgajdos83/rlock-mess
Browse files Browse the repository at this point in the history
Avoid recursive RLock()
  • Loading branch information
asim authored Oct 18, 2019
2 parents 63fd8b9 + 3d5d9be commit d3140c0
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions network/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ func (n *node) Nodes() []Node {
// GetPeerNode returns a node from node MaxDepth topology
// It returns nil if the peer was not found
func (n *node) GetPeerNode(id string) *node {
n.RLock()
defer n.RUnlock()

// get node topology up to MaxDepth
top := n.Topology(MaxDepth)

Expand Down Expand Up @@ -240,12 +237,9 @@ func (n *node) PruneStalePeerNodes(pruneTime time.Duration) map[string]*node {
return pruned
}

// Topology returns a copy of the node topology down to given depth
// NOTE: the returned node is a node graph - not a single node
func (n *node) Topology(depth uint) *node {
n.RLock()
defer n.RUnlock()

// getTopology traverses node graph and builds node topology
// NOTE: this function is not thread safe
func (n *node) getTopology(depth uint) *node {
// make a copy of yourself
node := &node{
id: n.id,
Expand All @@ -265,7 +259,7 @@ func (n *node) Topology(depth uint) *node {

// iterate through our peers and update the node peers
for _, peer := range n.peers {
nodePeer := peer.Topology(depth)
nodePeer := peer.getTopology(depth)
if _, ok := node.peers[nodePeer.id]; !ok {
node.peers[nodePeer.id] = nodePeer
}
Expand All @@ -274,14 +268,23 @@ func (n *node) Topology(depth uint) *node {
return node
}

// Topology returns a copy of the node topology down to given depth
// NOTE: the returned node is a node graph - not a single node
func (n *node) Topology(depth uint) *node {
n.RLock()
defer n.RUnlock()

return n.getTopology(depth)
}

// Peers returns node peers up to MaxDepth
func (n *node) Peers() []Node {
n.RLock()
defer n.RUnlock()

var peers []Node
for _, nodePeer := range n.peers {
peer := nodePeer.Topology(MaxDepth)
peer := nodePeer.getTopology(MaxDepth)
peers = append(peers, peer)
}

Expand Down

0 comments on commit d3140c0

Please sign in to comment.