Skip to content

Commit c5e4f8e

Browse files
committed
Revert "feat: message prioritization with immediate peer-published dispatch and queuing for other msgs (#1015)"
This reverts commit fe4ff79.
1 parent fe4ff79 commit c5e4f8e

File tree

7 files changed

+71
-196
lines changed

7 files changed

+71
-196
lines changed

libp2p/protocols/pubsub/floodsub.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ method rpcHandler*(f: FloodSub,
157157

158158
# In theory, if topics are the same in all messages, we could batch - we'd
159159
# also have to be careful to only include validated messages
160-
f.broadcast(toSendPeers, RPCMsg(messages: @[msg]), isHighPriority = false)
160+
f.broadcast(toSendPeers, RPCMsg(messages: @[msg]))
161161
trace "Forwared message to peers", peers = toSendPeers.len
162162

163163
f.updateMetrics(rpcMsg)
@@ -219,7 +219,7 @@ method publish*(f: FloodSub,
219219
return 0
220220

221221
# Try to send to all peers that are known to be interested
222-
f.broadcast(peers, RPCMsg(messages: @[msg]), isHighPriority = true)
222+
f.broadcast(peers, RPCMsg(messages: @[msg]))
223223

224224
when defined(libp2p_expensive_metrics):
225225
libp2p_pubsub_messages_published.inc(labelValues = [topic])

libp2p/protocols/pubsub/gossipsub.nim

+20-31
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ method unsubscribePeer*(g: GossipSub, peer: PeerId) =
220220
for topic, info in stats[].topicInfos.mpairs:
221221
info.firstMessageDeliveries = 0
222222

223-
pubSubPeer.stopSendNonPriorityTask()
224-
225223
procCall FloodSub(g).unsubscribePeer(peer)
226224

227225
proc handleSubscribe*(g: GossipSub,
@@ -281,40 +279,31 @@ proc handleControl(g: GossipSub, peer: PubSubPeer, control: ControlMessage) =
281279
respControl.prune.add(g.handleGraft(peer, control.graft))
282280
let messages = g.handleIWant(peer, control.iwant)
283281

284-
let
285-
isPruneNotEmpty = respControl.prune.len > 0
286-
isIWantNotEmpty = respControl.iwant.len > 0
287-
288-
if isPruneNotEmpty or isIWantNotEmpty:
289-
290-
if isIWantNotEmpty:
291-
libp2p_pubsub_broadcast_iwant.inc(respControl.iwant.len.int64)
292-
293-
if isPruneNotEmpty:
294-
for prune in respControl.prune:
295-
if g.knownTopics.contains(prune.topicId):
296-
libp2p_pubsub_broadcast_prune.inc(labelValues = [prune.topicId])
297-
else:
298-
libp2p_pubsub_broadcast_prune.inc(labelValues = ["generic"])
299-
300-
trace "sending control message", msg = shortLog(respControl), peer
301-
g.send(
302-
peer,
303-
RPCMsg(control: some(respControl)), isHighPriority = true)
282+
if
283+
respControl.prune.len > 0 or
284+
respControl.iwant.len > 0 or
285+
messages.len > 0:
286+
# iwant and prunes from here, also messages
304287

305-
if messages.len > 0:
306288
for smsg in messages:
307289
for topic in smsg.topicIds:
308290
if g.knownTopics.contains(topic):
309291
libp2p_pubsub_broadcast_messages.inc(labelValues = [topic])
310292
else:
311293
libp2p_pubsub_broadcast_messages.inc(labelValues = ["generic"])
312294

313-
# iwant replies have lower priority
314-
trace "sending iwant reply messages", peer
295+
libp2p_pubsub_broadcast_iwant.inc(respControl.iwant.len.int64)
296+
297+
for prune in respControl.prune:
298+
if g.knownTopics.contains(prune.topicId):
299+
libp2p_pubsub_broadcast_prune.inc(labelValues = [prune.topicId])
300+
else:
301+
libp2p_pubsub_broadcast_prune.inc(labelValues = ["generic"])
302+
303+
trace "sending control message", msg = shortLog(respControl), peer
315304
g.send(
316305
peer,
317-
RPCMsg(messages: messages), isHighPriority = false)
306+
RPCMsg(control: some(respControl), messages: messages))
318307

319308
proc validateAndRelay(g: GossipSub,
320309
msg: Message,
@@ -367,7 +356,7 @@ proc validateAndRelay(g: GossipSub,
367356
if msg.data.len > msgId.len * 10:
368357
g.broadcast(toSendPeers, RPCMsg(control: some(ControlMessage(
369358
idontwant: @[ControlIWant(messageIds: @[msgId])]
370-
))), isHighPriority = true)
359+
))))
371360

372361
for peer in toSendPeers:
373362
for heDontWant in peer.heDontWants:
@@ -381,7 +370,7 @@ proc validateAndRelay(g: GossipSub,
381370

382371
# In theory, if topics are the same in all messages, we could batch - we'd
383372
# also have to be careful to only include validated messages
384-
g.broadcast(toSendPeers, RPCMsg(messages: @[msg]), isHighPriority = false)
373+
g.broadcast(toSendPeers, RPCMsg(messages: @[msg]))
385374
trace "forwarded message to peers", peers = toSendPeers.len, msgId, peer
386375
for topic in msg.topicIds:
387376
if topic notin g.topics: continue
@@ -452,7 +441,7 @@ method rpcHandler*(g: GossipSub,
452441
peer.recvObservers(rpcMsg)
453442

454443
if rpcMsg.ping.len in 1..<64 and peer.pingBudget > 0:
455-
g.send(peer, RPCMsg(pong: rpcMsg.ping), isHighPriority = true)
444+
g.send(peer, RPCMsg(pong: rpcMsg.ping))
456445
peer.pingBudget.dec
457446
for i in 0..<min(g.topicsHigh, rpcMsg.subscriptions.len):
458447
template sub: untyped = rpcMsg.subscriptions[i]
@@ -562,7 +551,7 @@ method onTopicSubscription*(g: GossipSub, topic: string, subscribed: bool) =
562551
topicID: topic,
563552
peers: g.peerExchangeList(topic),
564553
backoff: g.parameters.unsubscribeBackoff.seconds.uint64)])))
565-
g.broadcast(mpeers, msg, isHighPriority = true)
554+
g.broadcast(mpeers, msg)
566555

567556
for peer in mpeers:
568557
g.pruned(peer, topic, backoff = some(g.parameters.unsubscribeBackoff))
@@ -666,7 +655,7 @@ method publish*(g: GossipSub,
666655

667656
g.mcache.put(msgId, msg)
668657

669-
g.broadcast(peers, RPCMsg(messages: @[msg]), isHighPriority = true)
658+
g.broadcast(peers, RPCMsg(messages: @[msg]))
670659

671660
if g.knownTopics.contains(topic):
672661
libp2p_pubsub_messages_published.inc(peers.len.int64, labelValues = [topic])

libp2p/protocols/pubsub/gossipsub/behavior.nim

+4-4
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,14 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil)
530530
# Send changes to peers after table updates to avoid stale state
531531
if grafts.len > 0:
532532
let graft = RPCMsg(control: some(ControlMessage(graft: @[ControlGraft(topicID: topic)])))
533-
g.broadcast(grafts, graft, isHighPriority = true)
533+
g.broadcast(grafts, graft)
534534
if prunes.len > 0:
535535
let prune = RPCMsg(control: some(ControlMessage(
536536
prune: @[ControlPrune(
537537
topicID: topic,
538538
peers: g.peerExchangeList(topic),
539539
backoff: g.parameters.pruneBackoff.seconds.uint64)])))
540-
g.broadcast(prunes, prune, isHighPriority = true)
540+
g.broadcast(prunes, prune)
541541

542542
proc dropFanoutPeers*(g: GossipSub) {.raises: [].} =
543543
# drop peers that we haven't published to in
@@ -669,7 +669,7 @@ proc onHeartbeat(g: GossipSub) {.raises: [].} =
669669
topicID: t,
670670
peers: g.peerExchangeList(t),
671671
backoff: g.parameters.pruneBackoff.seconds.uint64)])))
672-
g.broadcast(prunes, prune, isHighPriority = true)
672+
g.broadcast(prunes, prune)
673673

674674
# pass by ptr in order to both signal we want to update metrics
675675
# and as well update the struct for each topic during this iteration
@@ -691,7 +691,7 @@ proc onHeartbeat(g: GossipSub) {.raises: [].} =
691691
libp2p_pubsub_broadcast_ihave.inc(labelValues = [ihave.topicId])
692692
else:
693693
libp2p_pubsub_broadcast_ihave.inc(labelValues = ["generic"])
694-
g.send(peer, RPCMsg(control: some(control)), isHighPriority = true)
694+
g.send(peer, RPCMsg(control: some(control)))
695695

696696
g.mcache.shift() # shift the cache
697697

libp2p/protocols/pubsub/pubsub.nim

+8-24
Original file line numberDiff line numberDiff line change
@@ -138,34 +138,18 @@ method unsubscribePeer*(p: PubSub, peerId: PeerId) {.base, gcsafe.} =
138138

139139
libp2p_pubsub_peers.set(p.peers.len.int64)
140140

141-
proc send*(p: PubSub, peer: PubSubPeer, msg: RPCMsg, isHighPriority: bool) {.raises: [].} =
142-
## This procedure attempts to send a `msg` (of type `RPCMsg`) to the specified remote peer in the PubSub network.
141+
proc send*(p: PubSub, peer: PubSubPeer, msg: RPCMsg) {.raises: [].} =
142+
## Attempt to send `msg` to remote peer
143143
##
144-
## Parameters:
145-
## - `p`: The `PubSub` instance.
146-
## - `peer`: An instance of `PubSubPeer` representing the peer to whom the message should be sent.
147-
## - `msg`: The `RPCMsg` instance that contains the message to be sent.
148-
## - `isHighPriority`: A boolean indicating whether the message should be treated as high priority.
149-
## High priority messages are sent immediately, while low priority messages are queued and sent only after all high
150-
## priority messages have been sent.
151144

152145
trace "sending pubsub message to peer", peer, msg = shortLog(msg)
153-
asyncSpawn peer.send(msg, p.anonymize, isHighPriority)
146+
peer.send(msg, p.anonymize)
154147

155148
proc broadcast*(
156149
p: PubSub,
157150
sendPeers: auto, # Iteratble[PubSubPeer]
158-
msg: RPCMsg,
159-
isHighPriority: bool) {.raises: [].} =
160-
## This procedure attempts to send a `msg` (of type `RPCMsg`) to a specified group of peers in the PubSub network.
161-
##
162-
## Parameters:
163-
## - `p`: The `PubSub` instance.
164-
## - `sendPeers`: An iterable of `PubSubPeer` instances representing the peers to whom the message should be sent.
165-
## - `msg`: The `RPCMsg` instance that contains the message to be broadcast.
166-
## - `isHighPriority`: A boolean indicating whether the message should be treated as high priority.
167-
## High priority messages are sent immediately, while low priority messages are queued and sent only after all high
168-
## priority messages have been sent.
151+
msg: RPCMsg) {.raises: [].} =
152+
## Attempt to send `msg` to the given peers
169153

170154
let npeers = sendPeers.len.int64
171155
for sub in msg.subscriptions:
@@ -211,19 +195,19 @@ proc broadcast*(
211195

212196
if anyIt(sendPeers, it.hasObservers):
213197
for peer in sendPeers:
214-
p.send(peer, msg, isHighPriority)
198+
p.send(peer, msg)
215199
else:
216200
# Fast path that only encodes message once
217201
let encoded = encodeRpcMsg(msg, p.anonymize)
218202
for peer in sendPeers:
219-
asyncSpawn peer.sendEncoded(encoded, isHighPriority)
203+
asyncSpawn peer.sendEncoded(encoded)
220204

221205
proc sendSubs*(p: PubSub,
222206
peer: PubSubPeer,
223207
topics: openArray[string],
224208
subscribe: bool) =
225209
## send subscriptions to remote peer
226-
p.send(peer, RPCMsg.withSubs(topics, subscribe), isHighPriority = true)
210+
p.send(peer, RPCMsg.withSubs(topics, subscribe))
227211

228212
for topic in topics:
229213
if subscribe:

0 commit comments

Comments
 (0)