From abbeaab684c500f4c6ff5881797bb8f184b41ccc Mon Sep 17 00:00:00 2001 From: Tanguy Date: Tue, 6 Sep 2022 13:20:42 +0200 Subject: [PATCH] Keep connection alive when peer doesn't support pubsub (#754) --- libp2p/protocols/pubsub/pubsub.nim | 10 +--------- libp2p/protocols/pubsub/pubsubpeer.nim | 6 ------ tests/pubsub/testgossipinternal.nim | 5 +---- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index 823d792..606744a 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -292,19 +292,11 @@ proc getOrCreatePeer*( proc getConn(): Future[Connection] {.async.} = return await p.switch.dial(peerId, protos) - proc dropConn(peer: PubSubPeer) = - proc dropConnAsync(peer: PubSubPeer) {.async.} = - try: - await p.switch.disconnect(peer.peerId) - except CatchableError as exc: # never cancelled - trace "Failed to close connection", peer, error = exc.name, msg = exc.msg - asyncSpawn dropConnAsync(peer) - proc onEvent(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} = p.onPubSubPeerEvent(peer, event) # create new pubsub peer - let pubSubPeer = PubSubPeer.new(peerId, getConn, dropConn, onEvent, protos[0], p.maxMessageSize) + let pubSubPeer = PubSubPeer.new(peerId, getConn, onEvent, protos[0], p.maxMessageSize) debug "created new pubsub peer", peerId p.peers[peerId] = pubSubPeer diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 89f5733..8af49de 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -51,7 +51,6 @@ type PubSubPeer* = ref object of RootObj getConn*: GetConn # callback to establish a new send connection - dropConn*: DropConn # Function pointer to use to drop connections onEvent*: OnEvent # Connectivity updates for peer codec*: string # the protocol that this peer joined from sendConn*: Connection # cached send connection @@ -206,9 +205,6 @@ proc connectImpl(p: PubSubPeer) {.async.} = await connectOnce(p) except CatchableError as exc: # never cancelled debug "Could not establish send connection", msg = exc.msg - finally: - # drop the connection, else we end up with ghost peers - if p.dropConn != nil: p.dropConn(p) proc connect*(p: PubSubPeer) = asyncSpawn connectImpl(p) @@ -286,14 +282,12 @@ proc new*( T: typedesc[PubSubPeer], peerId: PeerId, getConn: GetConn, - dropConn: DropConn, onEvent: OnEvent, codec: string, maxMessageSize: int): T = T( getConn: getConn, - dropConn: dropConn, onEvent: onEvent, codec: codec, peerId: peerId, diff --git a/tests/pubsub/testgossipinternal.nim b/tests/pubsub/testgossipinternal.nim index 84f75e4..91ad4c0 100644 --- a/tests/pubsub/testgossipinternal.nim +++ b/tests/pubsub/testgossipinternal.nim @@ -22,10 +22,7 @@ proc getPubSubPeer(p: TestGossipSub, peerId: PeerId): PubSubPeer = proc getConn(): Future[Connection] = p.switch.dial(peerId, GossipSubCodec) - proc dropConn(peer: PubSubPeer) = - discard # we don't care about it here yet - - let pubSubPeer = PubSubPeer.new(peerId, getConn, dropConn, nil, GossipSubCodec, 1024 * 1024) + let pubSubPeer = PubSubPeer.new(peerId, getConn, nil, GossipSubCodec, 1024 * 1024) debug "created new pubsub peer", peerId p.peers[peerId] = pubSubPeer