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

View File

@ -425,8 +425,15 @@ proc newSwitch*(peerInfo: PeerInfo,
let s = result # can't capture result let s = result # can't capture result
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} = result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
trace "handling connection for", peerInfo = $stream.peerInfo try:
await s.ms.handle(stream) # handle incoming connection 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) result.mount(identity)
for key, val in muxers: for key, val in muxers: