From 451637a644c45d26f279ba3573177a3558686ab4 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 9 Feb 2024 16:11:43 +0100 Subject: [PATCH] await the last item --- libp2p/protocols/pubsub/pubsubpeer.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 23fc9fff1..ddc97d43c 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -375,8 +375,12 @@ proc sendNonPriorityTask(p: PubSubPeer) {.async.} = # we send non-priority messages only if there are no pending priority messages let msg = await p.rpcmessagequeue.nonPriorityQueue.popFirst() while p.rpcmessagequeue.sendPriorityQueue.len > 0: - await p.rpcmessagequeue.sendPriorityQueue[0] p.clearSendPriorityQueue() + # this minimizes the number of times we have to wait for something (each wait = performance cost) + # we will never wait for a finished future and by waiting for the last one, all that come before it are guaranteed + # to be finished already (since sends are processed in order). + if p.rpcmessagequeue.sendPriorityQueue.len > 0: + await p.rpcmessagequeue.sendPriorityQueue[^1] when defined(libp2p_expensive_metrics): libp2p_gossipsub_non_priority_queue_size.dec(labelValues = [$p.peerId]) await p.sendMsg(msg)