From 5f75e5adc6c66297de8dfbd09ae8a8171e959905 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 17 Jun 2020 20:12:32 -0600 Subject: [PATCH] don't use `switch.dial` in `subscribeToPeer` (#227) * count published messages * don't call `switch.dial` in `subscribeToPeer` --- libp2p/switch.nim | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 63025d28c..0f085000a 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -375,17 +375,24 @@ proc stop*(s: Switch) {.async.} = warn "error stopping switch", exc = exc.msg proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} = + trace "about to subscribe to pubsub peer", peer = peerInfo.shortLog() ## Subscribe to pub sub peer - if s.pubSub.isSome and peerInfo.id notin s.dialedPubSubPeers: + if s.pubSub.isSome and (peerInfo.id notin s.dialedPubSubPeers): + let conn = await s.getMuxedStream(peerInfo) try: - s.dialedPubSubPeers.incl(peerInfo.id) - let conn = await s.dial(peerInfo, s.pubSub.get().codec) if isNil(conn): - trace "unable to subscribe to peer" + trace "unable to subscribe to peer", peer = peerInfo.shortLog return - await s.pubSub.get().subscribeToPeer(conn) + + s.dialedPubSubPeers.incl(peerInfo.id) + if (await s.ms.select(conn, s.pubSub.get().codec)): + await s.pubSub.get().subscribeToPeer(conn) + else: + await conn.close() + except CatchableError as exc: - trace "exception in subscribe to peer", exc = exc.msg + trace "exception in subscribe to peer", peer = peerInfo.shortLog, exc = exc.msg + await conn.close() finally: s.dialedPubSubPeers.excl(peerInfo.id)