correct peerlifetime on disconnect

This commit is contained in:
Dmitriy Ryajov 2020-05-29 17:13:15 -06:00
parent ae0231ab5f
commit 2ecc699b6e
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 14 additions and 3 deletions

View File

@ -140,9 +140,8 @@ proc cleanupConn(s: Switch, conn: Connection) {.async, gcsafe.} =
s.muxed.del(id)
if id in s.connections:
if not s.connections[id].closed:
await s.connections[id].close()
s.connections.del(id)
await conn.close()
s.dialedPubSubPeers.excl(id)
@ -438,7 +437,7 @@ proc newSwitch*(peerInfo: PeerInfo,
if not(stream.closed):
await stream.close()
except CatchableError as exc:
trace "excepton in stream handler", exc = exc.msg
trace "exception in stream handler", exc = exc.msg
result.mount(identity)
for key, val in muxers:
@ -448,6 +447,8 @@ proc newSwitch*(peerInfo: PeerInfo,
try:
trace "got new muxer"
stream = await muxer.newStream()
# once we got a muxed connection, attempt to
# identify it
muxer.connection.peerInfo = await s.identify(stream)
# store muxer for connection
@ -456,6 +457,10 @@ proc newSwitch*(peerInfo: PeerInfo,
# store muxed connection
s.connections[muxer.connection.peerInfo.id] = muxer.connection
muxer.connection.closeEvent.wait()
.addCallback do(udata: pointer):
asyncCheck s.cleanupConn(muxer.connection)
# try establishing a pubsub connection
await s.subscribeToPeer(muxer.connection.peerInfo)
except CatchableError as exc:

View File

@ -192,6 +192,9 @@ suite "Switch":
awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo)
check switch1.connections.len > 0
check switch2.connections.len > 0
await sleepAsync(100.millis)
await switch2.disconnect(switch1.peerInfo)
@ -204,6 +207,9 @@ suite "Switch":
# echo connTracker.dump()
check connTracker.isLeaked() == false
check switch1.connections.len == 0
check switch2.connections.len == 0
await all(
switch1.stop(),
switch2.stop()