mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-02-28 12:30:46 +00:00
add more heartbeat locking to prevent races
This commit is contained in:
parent
0d0309a601
commit
ad7db1ca26
@ -220,10 +220,12 @@ method handleDisconnect*(g: GossipSub, peer: PubSubPeer) {.async.} =
|
|||||||
## handle peer disconnects
|
## handle peer disconnects
|
||||||
trace "peer disconnected", peer=peer.id
|
trace "peer disconnected", peer=peer.id
|
||||||
|
|
||||||
|
# must avoid running this while manipulating mesh/gossip tables
|
||||||
|
await g.heartbeatLock.acquire()
|
||||||
|
try:
|
||||||
await procCall FloodSub(g).handleDisconnect(peer)
|
await procCall FloodSub(g).handleDisconnect(peer)
|
||||||
|
|
||||||
for t in toSeq(g.gossipsub.keys):
|
for t in toSeq(g.gossipsub.keys):
|
||||||
if t in g.gossipsub:
|
|
||||||
g.gossipsub[t].excl(peer.id)
|
g.gossipsub[t].excl(peer.id)
|
||||||
|
|
||||||
libp2p_gossipsub_peers_per_topic_gossipsub
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
||||||
@ -233,18 +235,18 @@ method handleDisconnect*(g: GossipSub, peer: PubSubPeer) {.async.} =
|
|||||||
await procCall PubSub(g).subscribeTopic(t, false, peer.id)
|
await procCall PubSub(g).subscribeTopic(t, false, peer.id)
|
||||||
|
|
||||||
for t in toSeq(g.mesh.keys):
|
for t in toSeq(g.mesh.keys):
|
||||||
if t in g.mesh:
|
|
||||||
g.mesh[t].excl(peer.id)
|
g.mesh[t].excl(peer.id)
|
||||||
|
|
||||||
libp2p_gossipsub_peers_per_topic_mesh
|
libp2p_gossipsub_peers_per_topic_mesh
|
||||||
.set(g.mesh[t].len.int64, labelValues = [t])
|
.set(g.mesh[t].len.int64, labelValues = [t])
|
||||||
|
|
||||||
for t in toSeq(g.fanout.keys):
|
for t in toSeq(g.fanout.keys):
|
||||||
if t in g.fanout:
|
|
||||||
g.fanout[t].excl(peer.id)
|
g.fanout[t].excl(peer.id)
|
||||||
|
|
||||||
libp2p_gossipsub_peers_per_topic_fanout
|
libp2p_gossipsub_peers_per_topic_fanout
|
||||||
.set(g.fanout[t].len.int64, labelValues = [t])
|
.set(g.fanout[t].len.int64, labelValues = [t])
|
||||||
|
finally:
|
||||||
|
g.heartbeatLock.release()
|
||||||
|
|
||||||
method subscribeToPeer*(p: GossipSub,
|
method subscribeToPeer*(p: GossipSub,
|
||||||
conn: Connection) {.async.} =
|
conn: Connection) {.async.} =
|
||||||
@ -255,6 +257,10 @@ method subscribeTopic*(g: GossipSub,
|
|||||||
topic: string,
|
topic: string,
|
||||||
subscribe: bool,
|
subscribe: bool,
|
||||||
peerId: string) {.gcsafe, async.} =
|
peerId: string) {.gcsafe, async.} =
|
||||||
|
|
||||||
|
# must avoid running this while manipulating mesh/gossip tables
|
||||||
|
await g.heartbeatLock.acquire()
|
||||||
|
try:
|
||||||
await procCall PubSub(g).subscribeTopic(topic, subscribe, peerId)
|
await procCall PubSub(g).subscribeTopic(topic, subscribe, peerId)
|
||||||
|
|
||||||
if topic notin g.gossipsub:
|
if topic notin g.gossipsub:
|
||||||
@ -273,10 +279,8 @@ method subscribeTopic*(g: GossipSub,
|
|||||||
.set(g.gossipsub[topic].len.int64, labelValues = [topic])
|
.set(g.gossipsub[topic].len.int64, labelValues = [topic])
|
||||||
|
|
||||||
trace "gossip peers", peers = g.gossipsub[topic].len, topic
|
trace "gossip peers", peers = g.gossipsub[topic].len, topic
|
||||||
|
finally:
|
||||||
# also rebalance current topic if we are subbed to
|
g.heartbeatLock.release()
|
||||||
if topic in g.topics:
|
|
||||||
await g.rebalanceMesh(topic)
|
|
||||||
|
|
||||||
proc handleGraft(g: GossipSub,
|
proc handleGraft(g: GossipSub,
|
||||||
peer: PubSubPeer,
|
peer: PubSubPeer,
|
||||||
|
@ -100,7 +100,7 @@ method rpcHandler*(p: PubSub,
|
|||||||
method handleDisconnect*(p: PubSub, peer: PubSubPeer) {.async, base.} =
|
method handleDisconnect*(p: PubSub, peer: PubSubPeer) {.async, base.} =
|
||||||
## handle peer disconnects
|
## handle peer disconnects
|
||||||
if peer.id in p.peers:
|
if peer.id in p.peers:
|
||||||
trace "deleting peer", id = peer.id
|
trace "deleting peer", id = peer.id, trace = getStackTrace()
|
||||||
p.peers.del(peer.id)
|
p.peers.del(peer.id)
|
||||||
|
|
||||||
# metrics
|
# metrics
|
||||||
|
Loading…
x
Reference in New Issue
Block a user