close streams (#208)

* close streams

* don't warn on missing proto
This commit is contained in:
Dmitriy Ryajov 2020-06-08 17:42:27 -06:00 committed by GitHub
parent ee281310c0
commit 35ff99829e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -145,7 +145,7 @@ proc handle*(m: MultistreamSelect, conn: Connection) {.async, gcsafe.} =
await conn.writeLp((h.proto & "\n"))
await h.protocol.handler(conn, ms)
return
warn "no handlers for ", protocol = ms
debug "no handlers for ", protocol = ms
await conn.write(Na)
except CatchableError as exc:
trace "exception in multistream", exc = exc.msg

View File

@ -112,10 +112,15 @@ proc newIdentify*(peerInfo: PeerInfo): Identify =
method init*(p: Identify) =
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
try:
try:
trace "handling identify request"
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
await conn.writeLp(pb.buffer)
# await conn.close() #TODO: investigate why this breaks
finally:
await conn.close()
except CatchableError as exc:
trace "exception in identify handler", exc = exc.msg
p.handler = handle
p.codec = IdentifyCodec

View File

@ -110,6 +110,8 @@ proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} =
# new stream for identify
var stream = await muxer.newStream()
# call muxer handler, this should
# not end until muxer ends
let handlerFut = muxer.handle()
# add muxer handler cleanup proc
@ -117,10 +119,11 @@ proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} =
trace "muxer handler completed for peer",
peer = conn.peerInfo.id
try:
# do identify first, so that we have a
# PeerInfo in case we didn't before
conn.peerInfo = await s.identify(stream)
finally:
await stream.close() # close identify stream
trace "connection's peerInfo", peerInfo = $conn.peerInfo