mirror of https://github.com/vacp2p/nim-libp2p.git
remove all()
This commit is contained in:
parent
293b7da295
commit
815282a5da
|
@ -197,15 +197,28 @@ method close*(m: Mplex) {.async, gcsafe.} =
|
||||||
if m.isClosed:
|
if m.isClosed:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
trace "closing mplex muxer", oid = m.oid
|
trace "closing mplex muxer", oid = m.oid
|
||||||
await all(
|
let channs = toSeq(m.remote.values) &
|
||||||
toSeq(m.remote.values).mapIt(it.reset()) &
|
toSeq(m.local.values)
|
||||||
toSeq(m.local.values).mapIt(it.reset()))
|
|
||||||
|
|
||||||
await all(m.conns.mapIt(it.close())) # dispose of channe's connections
|
for chann in channs:
|
||||||
await all(m.handlerFuts)
|
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()
|
await m.connection.close()
|
||||||
|
finally:
|
||||||
m.remote.clear()
|
m.remote.clear()
|
||||||
m.local.clear()
|
m.local.clear()
|
||||||
m.conns = @[]
|
m.conns = @[]
|
||||||
|
|
|
@ -60,8 +60,7 @@ method init(c: MuxerProvider) =
|
||||||
if not isNil(c.muxerHandler):
|
if not isNil(c.muxerHandler):
|
||||||
futs &= c.muxerHandler(muxer)
|
futs &= c.muxerHandler(muxer)
|
||||||
|
|
||||||
# log and re-raise on errors
|
checkFutures(await allFinished(futs))
|
||||||
await all(futs)
|
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "exception in muxer handler", exc = exc.msg
|
trace "exception in muxer handler", exc = exc.msg
|
||||||
|
|
||||||
|
|
|
@ -328,22 +328,30 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
|
||||||
result = startFuts # listen for incoming connections
|
result = startFuts # listen for incoming connections
|
||||||
|
|
||||||
proc stop*(s: Switch) {.async.} =
|
proc stop*(s: Switch) {.async.} =
|
||||||
|
try:
|
||||||
trace "stopping switch"
|
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
|
# or crash here, cos we need to clean possibly MANY items
|
||||||
# and any following conn/transport won't be cleaned up
|
# and any following conn/transport won't be cleaned up
|
||||||
if s.pubSub.isSome:
|
if s.pubSub.isSome:
|
||||||
await s.pubSub.get().stop()
|
await s.pubSub.get().stop()
|
||||||
|
|
||||||
await all(
|
for conn in toSeq(s.connections.values):
|
||||||
toSeq(s.connections.values)
|
try:
|
||||||
.mapIt(s.cleanupConn(it)))
|
await s.cleanupConn(conn)
|
||||||
|
except CatchableError as exc:
|
||||||
|
warn "error cleaning up connections"
|
||||||
|
|
||||||
await all(
|
for t in s.transports:
|
||||||
s.transports.mapIt(it.close()))
|
try:
|
||||||
|
await t.close()
|
||||||
|
except CatchableError as exc:
|
||||||
|
warn "error cleaning up transports"
|
||||||
|
|
||||||
trace "switch stopped"
|
trace "switch stopped"
|
||||||
|
except CatchableError as exc:
|
||||||
|
warn "error stopping switch", exc = exc.msg
|
||||||
|
|
||||||
proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
|
proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
|
||||||
## Subscribe to pub sub peer
|
## Subscribe to pub sub peer
|
||||||
|
|
|
@ -113,12 +113,13 @@ method initTransport*(t: TcpTransport) =
|
||||||
inc getTcpTransportTracker().opened
|
inc getTcpTransportTracker().opened
|
||||||
|
|
||||||
method close*(t: TcpTransport) {.async, gcsafe.} =
|
method close*(t: TcpTransport) {.async, gcsafe.} =
|
||||||
|
try:
|
||||||
## start the transport
|
## start the transport
|
||||||
trace "stopping transport"
|
trace "stopping transport"
|
||||||
await procCall Transport(t).close() # call base
|
await procCall Transport(t).close() # call base
|
||||||
|
|
||||||
await all(
|
checkFutures(await allFinished(
|
||||||
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):
|
||||||
|
@ -130,18 +131,23 @@ 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()
|
||||||
await all(t.handlers)
|
|
||||||
|
checkFutures(
|
||||||
|
await allFinished(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()
|
||||||
await all(t.cleanups)
|
|
||||||
|
checkFutures(
|
||||||
|
await allFinished(t.cleanups))
|
||||||
t.cleanups = @[]
|
t.cleanups = @[]
|
||||||
|
|
||||||
trace "transport stopped"
|
trace "transport stopped"
|
||||||
|
|
||||||
inc getTcpTransportTracker().closed
|
inc getTcpTransportTracker().closed
|
||||||
|
except CatchableError as exc:
|
||||||
|
trace "error shutting down tcp transport", exc = exc.msg
|
||||||
|
|
||||||
method listen*(t: TcpTransport,
|
method listen*(t: TcpTransport,
|
||||||
ma: MultiAddress,
|
ma: MultiAddress,
|
||||||
|
|
Loading…
Reference in New Issue