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"
try:
while not conn.closed:
await sleepAsync(1.millis)
var ms = cast[string]((await conn.readLp()))
ms.removeSuffix("\n")

View File

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

View File

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