better cleanup

This commit is contained in:
Dmitriy Ryajov 2020-05-23 11:10:01 -06:00
parent 9c2f31262e
commit 95774b2b81
1 changed files with 17 additions and 5 deletions

View File

@ -7,13 +7,14 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import chronos, chronicles, sequtils import chronos, chronicles, sequtils, oids
import transport, import transport,
../errors, ../errors,
../wire, ../wire,
../connection, ../connection,
../multiaddress, ../multiaddress,
../multicodec, ../multicodec,
../stream/lpstream,
../stream/chronosstream ../stream/chronosstream
logScope: logScope:
@ -68,7 +69,18 @@ proc connHandler*(t: TcpTransport,
if not isNil(t.handler): if not isNil(t.handler):
t.handlers &= t.handler(conn) t.handlers &= t.handler(conn)
proc cleanup() {.async.} =
try:
await client.join()
trace "cleaning up client", addrs = client.remoteAddress, connoid = conn.oid
if not(isNil(conn)):
await conn.close()
t.clients.keepItIf(it != client)
except CatchableError as exc:
trace "error cleaning up client", exc = exc.msg
t.clients.add(client) t.clients.add(client)
asyncCheck cleanup()
result = conn result = conn
proc connCb(server: StreamServer, proc connCb(server: StreamServer,
@ -105,8 +117,8 @@ method close*(t: TcpTransport) {.async, gcsafe.} =
trace "stopping transport" trace "stopping transport"
await procCall Transport(t).close() # call base await procCall Transport(t).close() # call base
checkFutures(await allFinished( await all(
t.clients.mapIt(it.closeWait()))) t.clients.mapIt(it.closeWait()))
# server can be nil # server can be nil
if not isNil(t.server): if not isNil(t.server):
@ -118,13 +130,13 @@ method close*(t: TcpTransport) {.async, gcsafe.} =
for fut in t.handlers: for fut in t.handlers:
if not fut.finished: if not fut.finished:
fut.cancel() fut.cancel()
checkFutures(await allFinished(t.handlers)) await all(t.handlers)
t.handlers = @[] t.handlers = @[]
for fut in t.cleanups: for fut in t.cleanups:
if not fut.finished: if not fut.finished:
fut.cancel() fut.cancel()
checkFutures(await allFinished(t.cleanups)) await all(t.cleanups)
t.cleanups = @[] t.cleanups = @[]
trace "transport stopped" trace "transport stopped"