close and cleanup connections

This commit is contained in:
Dmitriy Ryajov 2020-01-07 02:02:37 -06:00
parent c6561b8851
commit cd8961cfb9
3 changed files with 49 additions and 47 deletions

View File

@ -112,7 +112,6 @@ proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
trace "handle: starting multistream handling" trace "handle: starting multistream handling"
try: try:
while not conn.closed: while not conn.closed:
await sleepAsync(1.millis)
var ms = cast[string]((await conn.readLp())) var ms = cast[string]((await conn.readLp()))
ms.removeSuffix("\n") ms.removeSuffix("\n")

View File

@ -125,7 +125,8 @@ method handle*(m: Mplex) {.async, gcsafe.} =
trace "exception occurred", exception = exc.msg trace "exception occurred", exception = exc.msg
finally: finally:
trace "stopping mplex main loop" trace "stopping mplex main loop"
await m.connection.close() if not m.connection.closed():
await m.connection.close()
proc newMplex*(conn: Connection, proc newMplex*(conn: Connection,
maxChanns: uint = MaxChannels): Mplex = maxChanns: uint = MaxChannels): Mplex =
@ -136,7 +137,8 @@ proc newMplex*(conn: Connection,
result.local = initTable[uint, LPChannel]() result.local = initTable[uint, LPChannel]()
let m = result let m = result
conn.closeEvent.wait().addCallback do (udata: pointer): conn.closeEvent.wait()
.addCallback do (udata: pointer):
trace "connection closed, cleaning up mplex" trace "connection closed, cleaning up mplex"
asyncCheck m.close() asyncCheck m.close()

View File

@ -350,7 +350,8 @@ proc `|`*(s: BufferStream, target: BufferStream): BufferStream =
method close*(s: BufferStream) {.async.} = method close*(s: BufferStream) {.async.} =
## close the stream and clear the buffer ## close the stream and clear the buffer
for r in s.readReqs: for r in s.readReqs:
r.cancel() if not(isNil(r)) and not(r.finished()):
r.cancel()
s.dataReadEvent.fire() s.dataReadEvent.fire()
s.readBuf.clear() s.readBuf.clear()
s.closeEvent.fire() s.closeEvent.fire()