Skip to content

Commit f7ae9fb

Browse files
committed
make maxDurationInNonPriorityQueue configurable and none by default
1 parent 38c0af4 commit f7ae9fb

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

libp2p/protocols/pubsub/gossipsub.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ proc init*(_: type[GossipSubParams]): GossipSubParams =
8383
enablePX: false,
8484
bandwidthEstimatebps: 100_000_000, # 100 Mbps or 12.5 MBps
8585
overheadRateLimit: Opt.none(tuple[bytes: int, interval: Duration]),
86-
disconnectPeerAboveRateLimit: false
86+
disconnectPeerAboveRateLimit: false,
87+
maxDurationInNonPriorityQueue: Opt.none(Duration),
8788
)
8889

8990
proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] =
@@ -746,4 +747,5 @@ method getOrCreatePeer*(
746747
let peer = procCall PubSub(g).getOrCreatePeer(peerId, protos)
747748
g.parameters.overheadRateLimit.withValue(overheadRateLimit):
748749
peer.overheadRateLimitOpt = Opt.some(TokenBucket.new(overheadRateLimit.bytes, overheadRateLimit.interval))
750+
peer.rpcmessagequeue.maxDurationInNonPriorityQueue = g.parameters.maxDurationInNonPriorityQueue
749751
return peer

libp2p/protocols/pubsub/gossipsub/types.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ type
147147
overheadRateLimit*: Opt[tuple[bytes: int, interval: Duration]]
148148
disconnectPeerAboveRateLimit*: bool
149149

150+
# The maximum duration a message can stay in the non-priority queue. If it exceeds this duration, it will be discarded
151+
# as soon as it is dequeued, instead of being sent to the remote peer. The default value is none, i.e., no maximum duration.
152+
maxDurationInNonPriorityQueue*: Opt[Duration]
153+
150154
BackoffTable* = Table[string, Table[PeerId, Moment]]
151155
ValidationSeenTable* = Table[MessageId, HashSet[PubSubPeer]]
152156

libp2p/protocols/pubsub/pubsubpeer.nim

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type
6767
# Task for processing non-priority message queue.
6868
sendNonPriorityTask: Future[void]
6969
# The max duration a message to be relayed can wait to be sent before it is dropped. The default is 500ms.
70-
maxDurationInNonPriorityQueue: Duration
70+
maxDurationInNonPriorityQueue*: Opt[Duration]
7171

7272
PubSubPeer* = ref object of RootObj
7373
getConn*: GetConn # callback to establish a new send connection
@@ -90,7 +90,7 @@ type
9090
behaviourPenalty*: float64 # the eventual penalty score
9191
overheadRateLimitOpt*: Opt[TokenBucket]
9292

93-
rpcmessagequeue: RpcMessageQueue
93+
rpcmessagequeue*: RpcMessageQueue
9494

9595
RPCHandler* = proc(peer: PubSubPeer, data: seq[byte]): Future[void]
9696
{.gcsafe, raises: [].}
@@ -388,10 +388,11 @@ proc sendNonPriorityTask(p: PubSubPeer) {.async.} =
388388
let ttlMsg = await p.rpcmessagequeue.nonPriorityQueue.popFirst()
389389
when defined(libp2p_expensive_metrics):
390390
libp2p_gossipsub_non_priority_queue_size.dec(labelValues = [$p.peerId])
391-
if Moment.now() - ttlMsg.addedAt >= p.rpcmessagequeue.maxDurationInNonPriorityQueue:
392-
when defined(libp2p_expensive_metrics):
393-
libp2p_gossipsub_non_priority_msgs_dropped.inc(labelValues = [$p.peerId])
394-
continue
391+
p.rpcmessagequeue.maxDurationInNonPriorityQueue.withValue(maxDurationInNonPriorityQueue):
392+
if Moment.now() - ttlMsg.addedAt >= maxDurationInNonPriorityQueue:
393+
when defined(libp2p_expensive_metrics):
394+
libp2p_gossipsub_non_priority_msgs_dropped.inc(labelValues = [$p.peerId])
395+
continue
395396
await p.sendMsg(ttlMsg.msg)
396397

397398
proc startSendNonPriorityTask(p: PubSubPeer) =
@@ -410,7 +411,7 @@ proc stopSendNonPriorityTask*(p: PubSubPeer) =
410411
libp2p_gossipsub_priority_queue_size.set(labelValues = [$p.peerId], value = 0)
411412
libp2p_gossipsub_non_priority_queue_size.set(labelValues = [$p.peerId], value = 0)
412413

413-
proc new(T: typedesc[RpcMessageQueue], maxDurationInNonPriorityQueue = 1.seconds): T =
414+
proc new(T: typedesc[RpcMessageQueue], maxDurationInNonPriorityQueue = Opt.none(Duration)): T =
414415
return T(
415416
sendPriorityQueue: initDeque[Future[void]](),
416417
nonPriorityQueue: newAsyncQueue[QueuedMessage](),
@@ -424,7 +425,8 @@ proc new*(
424425
onEvent: OnEvent,
425426
codec: string,
426427
maxMessageSize: int,
427-
overheadRateLimitOpt: Opt[TokenBucket] = Opt.none(TokenBucket)): T =
428+
overheadRateLimitOpt: Opt[TokenBucket] = Opt.none(TokenBucket),
429+
maxDurationInNonPriorityQueue = Opt.none(Duration)): T =
428430

429431
result = T(
430432
getConn: getConn,
@@ -434,7 +436,7 @@ proc new*(
434436
connectedFut: newFuture[void](),
435437
maxMessageSize: maxMessageSize,
436438
overheadRateLimitOpt: overheadRateLimitOpt,
437-
rpcmessagequeue: RpcMessageQueue.new(),
439+
rpcmessagequeue: RpcMessageQueue.new(maxDurationInNonPriorityQueue),
438440
)
439441
result.sentIHaves.addFirst(default(HashSet[MessageId]))
440442
result.heDontWants.addFirst(default(HashSet[MessageId]))

0 commit comments

Comments
 (0)