mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Avoid recursive calls to RLock()
Topology calls itsel recursively invoking RLock. This, according to go documentation is wrong. This commit moves the body of Topology function to a non-thread safe unexported function to keep locsk at check!
This commit is contained in:
parent
5c38f38dd9
commit
3d5d9be02a
@ -237,12 +237,9 @@ func (n *node) PruneStalePeerNodes(pruneTime time.Duration) map[string]*node {
|
|||||||
return pruned
|
return pruned
|
||||||
}
|
}
|
||||||
|
|
||||||
// Topology returns a copy of the node topology down to given depth
|
// getTopology traverses node graph and builds node topology
|
||||||
// NOTE: the returned node is a node graph - not a single node
|
// NOTE: this function is not thread safe
|
||||||
func (n *node) Topology(depth uint) *node {
|
func (n *node) getTopology(depth uint) *node {
|
||||||
n.RLock()
|
|
||||||
defer n.RUnlock()
|
|
||||||
|
|
||||||
// make a copy of yourself
|
// make a copy of yourself
|
||||||
node := &node{
|
node := &node{
|
||||||
id: n.id,
|
id: n.id,
|
||||||
@ -262,7 +259,7 @@ func (n *node) Topology(depth uint) *node {
|
|||||||
|
|
||||||
// iterate through our peers and update the node peers
|
// iterate through our peers and update the node peers
|
||||||
for _, peer := range n.peers {
|
for _, peer := range n.peers {
|
||||||
nodePeer := peer.Topology(depth)
|
nodePeer := peer.getTopology(depth)
|
||||||
if _, ok := node.peers[nodePeer.id]; !ok {
|
if _, ok := node.peers[nodePeer.id]; !ok {
|
||||||
node.peers[nodePeer.id] = nodePeer
|
node.peers[nodePeer.id] = nodePeer
|
||||||
}
|
}
|
||||||
@ -271,6 +268,15 @@ func (n *node) Topology(depth uint) *node {
|
|||||||
return 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
|
// Peers returns node peers up to MaxDepth
|
||||||
func (n *node) Peers() []Node {
|
func (n *node) Peers() []Node {
|
||||||
n.RLock()
|
n.RLock()
|
||||||
@ -278,7 +284,7 @@ func (n *node) Peers() []Node {
|
|||||||
|
|
||||||
var peers []Node
|
var peers []Node
|
||||||
for _, nodePeer := range n.peers {
|
for _, nodePeer := range n.peers {
|
||||||
peer := nodePeer.Topology(MaxDepth)
|
peer := nodePeer.getTopology(MaxDepth)
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user