remove all()

This commit is contained in:
Dmitriy Ryajov 2020-05-27 14:46:25 -06:00
parent 293b7da295
commit 815282a5da
4 changed files with 74 additions and 48 deletions

View File

@ -197,15 +197,28 @@ method close*(m: Mplex) {.async, gcsafe.} =
if m.isClosed:
return
try:
trace "closing mplex muxer", oid = m.oid
await all(
toSeq(m.remote.values).mapIt(it.reset()) &
toSeq(m.local.values).mapIt(it.reset()))
let channs = toSeq(m.remote.values) &
toSeq(m.local.values)
await all(m.conns.mapIt(it.close())) # dispose of channe's connections
await all(m.handlerFuts)
for chann in channs:
try:
await chann.reset()
except CatchableError as exc:
warn "error resetting channel", exc = exc.msg
for conn in m.conns:
try:
await conn.close()
except CatchableError as exc:
warn "error closing channel's connection"
checkFutures(
await allFinished(m.handlerFuts))
await m.connection.close()
finally:
m.remote.clear()
m.local.clear()
m.conns = @[]

View File

@ -60,8 +60,7 @@ method init(c: MuxerProvider) =
if not isNil(c.muxerHandler):
futs &= c.muxerHandler(muxer)
# log and re-raise on errors
await all(futs)
checkFutures(await allFinished(futs))
except CatchableError as exc:
trace "exception in muxer handler", exc = exc.msg

View File

@ -328,22 +328,30 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
result = startFuts # listen for incoming connections
proc stop*(s: Switch) {.async.} =
try:
trace "stopping switch"
# we want to report erros but we do not want to fail
# we want to report errors but we do not want to fail
# or crash here, cos we need to clean possibly MANY items
# and any following conn/transport won't be cleaned up
if s.pubSub.isSome:
await s.pubSub.get().stop()
await all(
toSeq(s.connections.values)
.mapIt(s.cleanupConn(it)))
for conn in toSeq(s.connections.values):
try:
await s.cleanupConn(conn)
except CatchableError as exc:
warn "error cleaning up connections"
await all(
s.transports.mapIt(it.close()))
for t in s.transports:
try:
await t.close()
except CatchableError as exc:
warn "error cleaning up transports"
trace "switch stopped"
except CatchableError as exc:
warn "error stopping switch", exc = exc.msg
proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
## Subscribe to pub sub peer

View File

@ -113,12 +113,13 @@ method initTransport*(t: TcpTransport) =
inc getTcpTransportTracker().opened
method close*(t: TcpTransport) {.async, gcsafe.} =
try:
## start the transport
trace "stopping transport"
await procCall Transport(t).close() # call base
await all(
t.clients.mapIt(it.closeWait()))
checkFutures(await allFinished(
t.clients.mapIt(it.closeWait())))
# server can be nil
if not isNil(t.server):
@ -130,18 +131,23 @@ method close*(t: TcpTransport) {.async, gcsafe.} =
for fut in t.handlers:
if not fut.finished:
fut.cancel()
await all(t.handlers)
checkFutures(
await allFinished(t.handlers))
t.handlers = @[]
for fut in t.cleanups:
if not fut.finished:
fut.cancel()
await all(t.cleanups)
checkFutures(
await allFinished(t.cleanups))
t.cleanups = @[]
trace "transport stopped"
inc getTcpTransportTracker().closed
except CatchableError as exc:
trace "error shutting down tcp transport", exc = exc.msg
method listen*(t: TcpTransport,
ma: MultiAddress,