move stram close to a better location

This commit is contained in:
Dmitriy Ryajov 2020-05-23 13:25:53 -06:00
parent daef00fc7b
commit abc12d0fb5
2 changed files with 14 additions and 9 deletions

View File

@ -49,7 +49,10 @@ proc newStreamInternal*(m: Mplex,
lazy: bool = false):
Future[LPChannel] {.async, gcsafe.} =
## create new channel/stream
let id = if initiator: m.currentId.inc(); m.currentId else: chanId
let id = if initiator:
m.currentId.inc(); m.currentId
else: chanId
trace "creating new channel", channelId = id,
initiator = initiator,
name = name,
@ -100,12 +103,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
var fut = newFuture[void]()
proc handler() {.async.} =
try:
try:
await m.streamHandler(stream)
trace "streamhandler ended", oid = stream.oid
finally:
if not(stream.closed):
await stream.close()
await m.streamHandler(stream)
except CatchableError as exc:
trace "exception in stream handler", exc = exc.msg
finally:

View File

@ -425,8 +425,15 @@ proc newSwitch*(peerInfo: PeerInfo,
let s = result # can't capture result
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
trace "handling connection for", peerInfo = $stream.peerInfo
await s.ms.handle(stream) # handle incoming connection
try:
trace "handling connection for", peerInfo = $stream.peerInfo
try:
await s.ms.handle(stream) # handle incoming connection
finally:
if not(stream.closed):
await stream.close()
except CatchableError as exc:
trace "excepton in stream handler", exc = exc.msg
result.mount(identity)
for key, val in muxers: