take into account explicit direct peers in publish

This commit is contained in:
Giovanni Petrantoni 2020-06-26 12:31:08 +09:00
parent 84988a147c
commit cf51da00b2
2 changed files with 30 additions and 24 deletions

View File

@ -524,40 +524,37 @@ method publish*(g: GossipSub,
await procCall PubSub(g).publish(topic, data) await procCall PubSub(g).publish(topic, data)
trace "about to publish message on topic", name = topic, trace "about to publish message on topic", name = topic,
data = data.shortLog data = data.shortLog
# directly copy explicit peers
# as we will always publish to those
var peers = g.explicitPeers
var peers: HashSet[string]
# TODO: we probably don't need to try multiple times
if data.len > 0 and topic.len > 0: if data.len > 0 and topic.len > 0:
for _ in 0..<5: # try to get peers up to 5 times
if peers.len > 0:
break
if topic in g.topics: # if we're subscribed to the topic attempt to build a mesh if topic in g.topics: # if we're subscribed to the topic attempt to build a mesh
await g.rebalanceMesh(topic) await g.rebalanceMesh(topic)
peers = g.mesh.getOrDefault(topic) peers.incl(g.mesh.getOrDefault(topic))
else: # send to fanout peers else: # send to fanout peers
await g.replenishFanout(topic) await g.replenishFanout(topic)
if topic in g.fanout: if topic in g.fanout:
peers = g.fanout.getOrDefault(topic) peers.incl(g.fanout.getOrDefault(topic))
# set the fanout expiry time # set the fanout expiry time
g.lastFanoutPubSub[topic] = Moment.fromNow(GossipSubFanoutTTL) g.lastFanoutPubSub[topic] = Moment.fromNow(GossipSubFanoutTTL)
# wait a second between tries
await sleepAsync(1.seconds)
let msg = newMessage(g.peerInfo, data, topic, g.sign) let msg = newMessage(g.peerInfo, data, topic, g.sign)
trace "created new message", msg trace "created new message", msg
var sent: seq[Future[void]]
for p in peers:
if p == g.peerInfo.id:
continue
trace "publishing on topic", name = topic trace "publishing on topic", name = topic
if msg.msgId notin g.mcache: if msg.msgId notin g.mcache:
g.mcache.put(msg) g.mcache.put(msg)
if p in g.peers: var sent: seq[Future[void]]
for p in peers:
# avoid sending to self
if p == g.peerInfo.id:
continue
let peer = g.peers.getOrDefault(p)
if not isNil(peer.peerInfo):
sent.add(g.peers[p].send(@[RPCMsg(messages: @[msg])])) sent.add(g.peers[p].send(@[RPCMsg(messages: @[msg])]))
checkFutures(await allFinished(sent)) checkFutures(await allFinished(sent))
libp2p_pubsub_messages_published.inc(labelValues = [topic]) libp2p_pubsub_messages_published.inc(labelValues = [topic])

View File

@ -27,6 +27,15 @@ declareCounter(libp2p_pubsub_skipped_received_messages, "number of received skip
declareCounter(libp2p_pubsub_skipped_sent_messages, "number of sent skipped messages", labels = ["id"]) declareCounter(libp2p_pubsub_skipped_sent_messages, "number of sent skipped messages", labels = ["id"])
type type
TopicScoreParams = object
topicWeight: float
timeInMeshWeight: float
timeInMeshQuantum: Duration
timeInMeshCap: float
PeerScoreParams = object
topics: Table[string, TopicScoreParams]
PubSubObserver* = ref object PubSubObserver* = ref object
onRecv*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].} onRecv*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].}
onSend*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].} onSend*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].}