merge 1.0

This commit is contained in:
Giovanni Petrantoni 2020-07-04 20:53:40 +09:00
parent 2f39cf6f42
commit 69a92aebe6
1 changed files with 71 additions and 78 deletions

View File

@ -242,6 +242,7 @@ proc getGossipPeers(g: GossipSub): Table[string, ControlMessage] {.gcsafe.} =
proc heartbeat(g: GossipSub) {.async.} = proc heartbeat(g: GossipSub) {.async.} =
while g.heartbeatRunning: while g.heartbeatRunning:
withLock g.heartbeatLock:
try: try:
trace "running heartbeat" trace "running heartbeat"
@ -275,6 +276,7 @@ method handleDisconnect*(g: GossipSub, peer: PubSubPeer) {.async.} =
await procCall FloodSub(g).handleDisconnect(peer) await procCall FloodSub(g).handleDisconnect(peer)
withLock g.heartbeatLock:
for t in toSeq(g.gossipsub.keys): for t in toSeq(g.gossipsub.keys):
if t in g.gossipsub: if t in g.gossipsub:
g.gossipsub[t].excl(peer.id) g.gossipsub[t].excl(peer.id)
@ -310,6 +312,7 @@ method subscribeTopic*(g: GossipSub,
peerId: string) {.gcsafe, async.} = peerId: string) {.gcsafe, async.} =
await procCall PubSub(g).subscribeTopic(topic, subscribe, peerId) await procCall PubSub(g).subscribeTopic(topic, subscribe, peerId)
withLock g.heartbeatLock:
if topic notin g.gossipsub: if topic notin g.gossipsub:
g.gossipsub[topic] = initHashSet[string]() g.gossipsub[topic] = initHashSet[string]()
@ -555,14 +558,11 @@ method publish*(g: GossipSub,
trace "publish: sending message to peer", peer = p trace "publish: sending message to peer", peer = p
sent.add(peer.send(@[RPCMsg(messages: @[msg])])) sent.add(peer.send(@[RPCMsg(messages: @[msg])]))
else: else:
# Notice this needs a better fix! for now it's a hack # this absolutely should not happen
error "publish: peer or peerInfo was nil", missing = p # if it happens there is a bug that needs fixing asap
if topic in g.mesh: # this ain't no place to manage connections
g.mesh[topic].excl(p) fatal "publish: peer or peerInfo was nil", missing = p
if topic in g.fanout: doAssert(false, "publish: peer or peerInfo was nil")
g.fanout[topic].excl(p)
if topic in g.gossipsub:
g.gossipsub[topic].excl(p)
sent = await allFinished(sent) sent = await allFinished(sent)
checkFutures(sent) checkFutures(sent)
@ -580,31 +580,24 @@ method start*(g: GossipSub) {.async.} =
## start pubsub ## start pubsub
## start long running/repeating procedures ## start long running/repeating procedures
# interlock start to to avoid overlapping to stops withLock g.heartbeatLock:
await g.heartbeatLock.acquire()
# setup the heartbeat interval # setup the heartbeat interval
g.heartbeatRunning = true g.heartbeatRunning = true
g.heartbeatFut = g.heartbeat() g.heartbeatFut = g.heartbeat()
g.heartbeatLock.release()
method stop*(g: GossipSub) {.async.} = method stop*(g: GossipSub) {.async.} =
trace "gossipsub stop" trace "gossipsub stop"
## stop pubsub ## stop pubsub
## stop long running tasks ## stop long running tasks
await g.heartbeatLock.acquire() withLock g.heartbeatLock:
# stop heartbeat interval # stop heartbeat interval
g.heartbeatRunning = false g.heartbeatRunning = false
if not g.heartbeatFut.finished: if not g.heartbeatFut.finished:
trace "awaiting last heartbeat" trace "awaiting last heartbeat"
await g.heartbeatFut await g.heartbeatFut
g.heartbeatLock.release()
method initPubSub*(g: GossipSub) = method initPubSub*(g: GossipSub) =
procCall FloodSub(g).initPubSub() procCall FloodSub(g).initPubSub()