From ca01ee06a8a90567e9ac13035ef2d18b1a2a1254 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Wed, 6 Mar 2024 06:49:45 +0100 Subject: [PATCH] clean up triple lookup and avoid `KeyError` when adding muxer (#1057) --- libp2p/connmanager.nim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libp2p/connmanager.nim b/libp2p/connmanager.nim index ba69caac5..210dc660b 100644 --- a/libp2p/connmanager.nim +++ b/libp2p/connmanager.nim @@ -311,12 +311,14 @@ proc storeMuxer*(c: ConnManager, raise newTooManyConnectionsError() - assert muxer notin c.muxed.getOrDefault(peerId) - - let - newPeer = peerId notin c.muxed - assert newPeer or c.muxed[peerId].len > 0 - c.muxed.mgetOrPut(peerId, newSeq[Muxer]()).add(muxer) + var newPeer = false + c.muxed.withValue(peerId, muxers): + doAssert muxers[].len > 0 + doAssert muxer notin muxers[] + muxers[].add(muxer) + do: + c.muxed[peerId] = @[muxer] + newPeer = true libp2p_peers.set(c.muxed.len.int64) asyncSpawn c.triggerConnEvent(