2020-07-17 15:36:48 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2020 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
import std/[options, tables, sequtils, sets]
|
2020-07-17 15:36:48 +00:00
|
|
|
import chronos, chronicles, metrics
|
|
|
|
import peerinfo,
|
|
|
|
stream/connection,
|
2020-09-23 14:07:16 +00:00
|
|
|
muxers/muxer,
|
|
|
|
errors
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-09-15 06:03:53 +00:00
|
|
|
logScope:
|
|
|
|
topics = "connmanager"
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
declareGauge(libp2p_peers, "total connected peers")
|
|
|
|
|
|
|
|
const MaxConnectionsPerPeer = 5
|
|
|
|
|
|
|
|
type
|
|
|
|
TooManyConnections* = object of CatchableError
|
|
|
|
|
2020-09-23 14:07:16 +00:00
|
|
|
ConnEventKind* {.pure.} = enum
|
|
|
|
Connected, # A connection was made and securely upgraded - there may be
|
|
|
|
# more than one concurrent connection thus more than one upgrade
|
|
|
|
# event per peer.
|
|
|
|
|
|
|
|
Disconnected # Peer disconnected - this event is fired once per upgrade
|
|
|
|
# when the associated connection is terminated.
|
|
|
|
|
|
|
|
ConnEvent* = object
|
|
|
|
case kind*: ConnEventKind
|
|
|
|
of ConnEventKind.Connected:
|
|
|
|
incoming*: bool
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
ConnEventHandler* =
|
|
|
|
proc(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.}
|
|
|
|
|
|
|
|
PeerEvent* {.pure.} = enum
|
|
|
|
Left,
|
|
|
|
Joined
|
|
|
|
|
|
|
|
PeerEventHandler* =
|
|
|
|
proc(peerId: PeerID, event: PeerEvent): Future[void] {.gcsafe.}
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
MuxerHolder = object
|
|
|
|
muxer: Muxer
|
|
|
|
handle: Future[void]
|
|
|
|
|
|
|
|
ConnManager* = ref object of RootObj
|
2020-09-23 14:07:16 +00:00
|
|
|
maxConns: int
|
2020-07-17 15:36:48 +00:00
|
|
|
# NOTE: don't change to PeerInfo here
|
|
|
|
# the reference semantics on the PeerInfo
|
2020-09-15 20:19:22 +00:00
|
|
|
# object itself make it susceptible to
|
2020-07-17 15:36:48 +00:00
|
|
|
# copies and mangling by unrelated code.
|
|
|
|
conns: Table[PeerID, HashSet[Connection]]
|
|
|
|
muxed: Table[Connection, MuxerHolder]
|
2020-09-23 14:07:16 +00:00
|
|
|
connEvents: Table[ConnEventKind, OrderedSet[ConnEventHandler]]
|
|
|
|
peerEvents: Table[PeerEvent, OrderedSet[PeerEventHandler]]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
proc newTooManyConnections(): ref TooManyConnections {.inline.} =
|
|
|
|
result = newException(TooManyConnections, "too many connections for peer")
|
|
|
|
|
|
|
|
proc init*(C: type ConnManager,
|
|
|
|
maxConnsPerPeer: int = MaxConnectionsPerPeer): ConnManager =
|
|
|
|
C(maxConns: maxConnsPerPeer,
|
|
|
|
conns: initTable[PeerID, HashSet[Connection]](),
|
|
|
|
muxed: initTable[Connection, MuxerHolder]())
|
|
|
|
|
2020-09-23 14:07:16 +00:00
|
|
|
proc connCount*(c: ConnManager, peerId: PeerID): int =
|
|
|
|
c.conns.getOrDefault(peerId).len
|
|
|
|
|
|
|
|
proc addConnEventHandler*(c: ConnManager,
|
|
|
|
handler: ConnEventHandler, kind: ConnEventKind) =
|
|
|
|
## Add peer event handler - handlers must not raise exceptions!
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(handler): return
|
|
|
|
c.connEvents.mgetOrPut(kind,
|
|
|
|
initOrderedSet[ConnEventHandler]()).incl(handler)
|
|
|
|
|
|
|
|
proc removeConnEventHandler*(c: ConnManager,
|
|
|
|
handler: ConnEventHandler, kind: ConnEventKind) =
|
|
|
|
c.connEvents.withValue(kind, handlers) do:
|
|
|
|
handlers[].excl(handler)
|
|
|
|
|
|
|
|
proc triggerConnEvent*(c: ConnManager, peerId: PeerID, event: ConnEvent) {.async, gcsafe.} =
|
|
|
|
try:
|
|
|
|
if event.kind in c.connEvents:
|
|
|
|
var connEvents: seq[Future[void]]
|
|
|
|
for h in c.connEvents[event.kind]:
|
|
|
|
connEvents.add(h(peerId, event))
|
|
|
|
|
|
|
|
checkFutures(await allFinished(connEvents))
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc: # handlers should not raise!
|
|
|
|
warn "Exception in triggerConnEvents",
|
|
|
|
msg = exc.msg, peerId, event = $event
|
|
|
|
|
|
|
|
proc addPeerEventHandler*(c: ConnManager,
|
|
|
|
handler: PeerEventHandler,
|
|
|
|
kind: PeerEvent) =
|
|
|
|
## Add peer event handler - handlers must not raise exceptions!
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(handler): return
|
|
|
|
c.peerEvents.mgetOrPut(kind,
|
|
|
|
initOrderedSet[PeerEventHandler]()).incl(handler)
|
|
|
|
|
|
|
|
proc removePeerEventHandler*(c: ConnManager,
|
|
|
|
handler: PeerEventHandler,
|
|
|
|
kind: PeerEvent) =
|
|
|
|
c.peerEvents.withValue(kind, handlers) do:
|
|
|
|
handlers[].excl(handler)
|
|
|
|
|
|
|
|
proc triggerPeerEvents*(c: ConnManager,
|
|
|
|
peerId: PeerID,
|
|
|
|
event: PeerEvent) {.async, gcsafe.} =
|
|
|
|
|
2020-11-02 20:35:26 +00:00
|
|
|
trace "About to trigger peer events", peer = peerId
|
2020-09-23 14:07:16 +00:00
|
|
|
if event notin c.peerEvents:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
let count = c.connCount(peerId)
|
|
|
|
if event == PeerEvent.Joined and count != 1:
|
2020-11-02 20:35:26 +00:00
|
|
|
trace "peer already joined", peerId, event = $event
|
2020-09-23 14:07:16 +00:00
|
|
|
return
|
|
|
|
elif event == PeerEvent.Left and count != 0:
|
2020-11-02 20:35:26 +00:00
|
|
|
trace "peer still connected or already left", peerId, event = $event
|
2020-09-23 14:07:16 +00:00
|
|
|
return
|
|
|
|
|
2020-11-02 20:35:26 +00:00
|
|
|
trace "triggering peer events", peerId, event = $event
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
var peerEvents: seq[Future[void]]
|
|
|
|
for h in c.peerEvents[event]:
|
|
|
|
peerEvents.add(h(peerId, event))
|
|
|
|
|
|
|
|
checkFutures(await allFinished(peerEvents))
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc: # handlers should not raise!
|
2020-11-02 20:35:26 +00:00
|
|
|
warn "Exception in triggerPeerEvents", exc = exc.msg, peerId
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc contains*(c: ConnManager, conn: Connection): bool =
|
|
|
|
## checks if a connection is being tracked by the
|
|
|
|
## connection manager
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(conn):
|
|
|
|
return
|
|
|
|
|
|
|
|
if isNil(conn.peerInfo):
|
|
|
|
return
|
|
|
|
|
|
|
|
return conn in c.conns[conn.peerInfo.peerId]
|
|
|
|
|
|
|
|
proc contains*(c: ConnManager, peerId: PeerID): bool =
|
|
|
|
peerId in c.conns
|
|
|
|
|
|
|
|
proc contains*(c: ConnManager, muxer: Muxer): bool =
|
|
|
|
## checks if a muxer is being tracked by the connection
|
|
|
|
## manager
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(muxer):
|
|
|
|
return
|
|
|
|
|
|
|
|
let conn = muxer.connection
|
|
|
|
if conn notin c:
|
|
|
|
return
|
|
|
|
|
|
|
|
if conn notin c.muxed:
|
|
|
|
return
|
|
|
|
|
|
|
|
return muxer == c.muxed[conn].muxer
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
proc closeMuxerHolder(muxerHolder: MuxerHolder) {.async.} =
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Cleaning up muxer", m = muxerHolder.muxer
|
2020-08-01 20:50:40 +00:00
|
|
|
|
|
|
|
await muxerHolder.muxer.close()
|
|
|
|
if not(isNil(muxerHolder.handle)):
|
|
|
|
await muxerHolder.handle # TODO noraises?
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Cleaned up muxer", m = muxerHolder.muxer
|
2020-08-01 20:50:40 +00:00
|
|
|
|
|
|
|
proc delConn(c: ConnManager, conn: Connection) =
|
|
|
|
let peerId = conn.peerInfo.peerId
|
|
|
|
if peerId in c.conns:
|
|
|
|
c.conns[peerId].excl(conn)
|
|
|
|
|
|
|
|
if c.conns[peerId].len == 0:
|
|
|
|
c.conns.del(peerId)
|
|
|
|
libp2p_peers.set(c.conns.len.int64)
|
|
|
|
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Removed connection", conn
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc cleanupConn(c: ConnManager, conn: Connection) {.async.} =
|
|
|
|
## clean connection's resources such as muxers and streams
|
|
|
|
|
|
|
|
if isNil(conn):
|
|
|
|
return
|
|
|
|
|
|
|
|
if isNil(conn.peerInfo):
|
|
|
|
return
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
# Remove connection from all tables without async breaks
|
|
|
|
var muxer = some(MuxerHolder())
|
|
|
|
if not c.muxed.pop(conn, muxer.get()):
|
|
|
|
muxer = none(MuxerHolder)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
delConn(c, conn)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
try:
|
|
|
|
if muxer.isSome:
|
|
|
|
await closeMuxerHolder(muxer.get())
|
2020-07-17 15:36:48 +00:00
|
|
|
finally:
|
|
|
|
await conn.close()
|
|
|
|
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Connection cleaned up", conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-02 20:35:26 +00:00
|
|
|
proc peerStartup(c: ConnManager, conn: Connection) {.async.} =
|
|
|
|
try:
|
2020-11-11 19:20:14 +00:00
|
|
|
trace "Triggering connect events", conn
|
2020-11-02 20:35:26 +00:00
|
|
|
let peerId = conn.peerInfo.peerId
|
|
|
|
await c.triggerPeerEvents(peerId, PeerEvent.Joined)
|
|
|
|
await c.triggerConnEvent(
|
|
|
|
peerId, ConnEvent(kind: ConnEventKind.Connected, incoming: conn.dir == Direction.In))
|
|
|
|
except CatchableError as exc:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propagate CancelledError and should handle other errors
|
|
|
|
warn "Unexpected exception in switch peer connection cleanup",
|
|
|
|
conn, msg = exc.msg
|
|
|
|
|
|
|
|
proc peerCleanup(c: ConnManager, conn: Connection) {.async.} =
|
|
|
|
try:
|
2020-11-11 19:20:14 +00:00
|
|
|
trace "Triggering disconnect events", conn
|
2020-11-02 20:35:26 +00:00
|
|
|
let peerId = conn.peerInfo.peerId
|
|
|
|
await c.triggerConnEvent(
|
|
|
|
peerId, ConnEvent(kind: ConnEventKind.Disconnected))
|
|
|
|
await c.triggerPeerEvents(peerId, PeerEvent.Left)
|
|
|
|
except CatchableError as exc:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propagate CancelledError and should handle other errors
|
|
|
|
warn "Unexpected exception peer cleanup handler",
|
|
|
|
conn, msg = exc.msg
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc onClose(c: ConnManager, conn: Connection) {.async.} =
|
|
|
|
## connection close even handler
|
|
|
|
##
|
|
|
|
## triggers the connections resource cleanup
|
|
|
|
##
|
2020-09-04 16:30:45 +00:00
|
|
|
try:
|
|
|
|
await conn.join()
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Connection closed, cleaning up", conn
|
2020-09-04 16:30:45 +00:00
|
|
|
await c.cleanupConn(conn)
|
|
|
|
except CancelledError:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
2020-11-02 20:35:26 +00:00
|
|
|
# do not need to propagate CancelledError.
|
2020-09-08 06:24:28 +00:00
|
|
|
debug "Unexpected cancellation in connection manager's cleanup", conn
|
2020-09-04 16:30:45 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-08 06:24:28 +00:00
|
|
|
debug "Unexpected exception in connection manager's cleanup",
|
|
|
|
errMsg = exc.msg, conn
|
2020-11-02 20:35:26 +00:00
|
|
|
finally:
|
|
|
|
trace "Triggering peerCleanup", conn
|
|
|
|
asyncSpawn c.peerCleanup(conn)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
proc selectConn*(c: ConnManager,
|
2020-08-01 20:50:40 +00:00
|
|
|
peerId: PeerID,
|
2020-07-17 15:36:48 +00:00
|
|
|
dir: Direction): Connection =
|
|
|
|
## Select a connection for the provided peer and direction
|
|
|
|
##
|
|
|
|
let conns = toSeq(
|
2020-08-01 20:50:40 +00:00
|
|
|
c.conns.getOrDefault(peerId))
|
2020-07-17 15:36:48 +00:00
|
|
|
.filterIt( it.dir == dir )
|
|
|
|
|
|
|
|
if conns.len > 0:
|
|
|
|
return conns[0]
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
proc selectConn*(c: ConnManager, peerId: PeerID): Connection =
|
2020-07-17 15:36:48 +00:00
|
|
|
## Select a connection for the provided giving priority
|
|
|
|
## to outgoing connections
|
|
|
|
##
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
var conn = c.selectConn(peerId, Direction.Out)
|
2020-07-17 15:36:48 +00:00
|
|
|
if isNil(conn):
|
2020-08-01 20:50:40 +00:00
|
|
|
conn = c.selectConn(peerId, Direction.In)
|
2020-08-15 19:50:31 +00:00
|
|
|
if isNil(conn):
|
|
|
|
trace "connection not found", peerId
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
proc selectMuxer*(c: ConnManager, conn: Connection): Muxer =
|
|
|
|
## select the muxer for the provided connection
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(conn):
|
|
|
|
return
|
|
|
|
|
|
|
|
if conn in c.muxed:
|
|
|
|
return c.muxed[conn].muxer
|
2020-08-15 19:50:31 +00:00
|
|
|
else:
|
2020-09-06 08:31:47 +00:00
|
|
|
debug "no muxer for connection", conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
proc storeConn*(c: ConnManager, conn: Connection) =
|
|
|
|
## store a connection
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(conn):
|
|
|
|
raise newException(CatchableError, "connection cannot be nil")
|
|
|
|
|
|
|
|
if isNil(conn.peerInfo):
|
|
|
|
raise newException(CatchableError, "empty peer info")
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
let peerId = conn.peerInfo.peerId
|
|
|
|
if c.conns.getOrDefault(peerId).len > c.maxConns:
|
2020-09-09 17:12:08 +00:00
|
|
|
debug "too many connections",
|
|
|
|
conn, conns = c.conns.getOrDefault(peerId).len
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
raise newTooManyConnections()
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
if peerId notin c.conns:
|
|
|
|
c.conns[peerId] = initHashSet[Connection]()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
c.conns[peerId].incl(conn)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-09-04 16:30:45 +00:00
|
|
|
# Launch on close listener
|
|
|
|
# All the errors are handled inside `onClose()` procedure.
|
|
|
|
asyncSpawn c.onClose(conn)
|
2020-07-17 15:36:48 +00:00
|
|
|
libp2p_peers.set(c.conns.len.int64)
|
|
|
|
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Stored connection",
|
2020-09-09 17:12:08 +00:00
|
|
|
conn, direction = $conn.dir, connections = c.conns.len
|
2020-08-15 19:50:31 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc storeOutgoing*(c: ConnManager, conn: Connection) =
|
|
|
|
conn.dir = Direction.Out
|
|
|
|
c.storeConn(conn)
|
|
|
|
|
|
|
|
proc storeIncoming*(c: ConnManager, conn: Connection) =
|
|
|
|
conn.dir = Direction.In
|
|
|
|
c.storeConn(conn)
|
|
|
|
|
|
|
|
proc storeMuxer*(c: ConnManager,
|
|
|
|
muxer: Muxer,
|
|
|
|
handle: Future[void] = nil) =
|
|
|
|
## store the connection and muxer
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(muxer):
|
|
|
|
raise newException(CatchableError, "muxer cannot be nil")
|
|
|
|
|
|
|
|
if isNil(muxer.connection):
|
|
|
|
raise newException(CatchableError, "muxer's connection cannot be nil")
|
|
|
|
|
|
|
|
c.muxed[muxer.connection] = MuxerHolder(
|
|
|
|
muxer: muxer,
|
|
|
|
handle: handle)
|
|
|
|
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Stored muxer",
|
|
|
|
muxer, handle = not handle.isNil, connections = c.conns.len
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-02 20:35:26 +00:00
|
|
|
asyncSpawn c.peerStartup(muxer.connection)
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc getMuxedStream*(c: ConnManager,
|
2020-08-01 20:50:40 +00:00
|
|
|
peerId: PeerID,
|
2020-07-17 15:36:48 +00:00
|
|
|
dir: Direction): Future[Connection] {.async, gcsafe.} =
|
|
|
|
## get a muxed stream for the provided peer
|
|
|
|
## with the given direction
|
|
|
|
##
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
let muxer = c.selectMuxer(c.selectConn(peerId, dir))
|
2020-07-17 15:36:48 +00:00
|
|
|
if not(isNil(muxer)):
|
|
|
|
return await muxer.newStream()
|
|
|
|
|
|
|
|
proc getMuxedStream*(c: ConnManager,
|
2020-08-01 20:50:40 +00:00
|
|
|
peerId: PeerID): Future[Connection] {.async, gcsafe.} =
|
2020-07-17 15:36:48 +00:00
|
|
|
## get a muxed stream for the passed peer from any connection
|
|
|
|
##
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
let muxer = c.selectMuxer(c.selectConn(peerId))
|
2020-07-17 15:36:48 +00:00
|
|
|
if not(isNil(muxer)):
|
|
|
|
return await muxer.newStream()
|
|
|
|
|
|
|
|
proc getMuxedStream*(c: ConnManager,
|
|
|
|
conn: Connection): Future[Connection] {.async, gcsafe.} =
|
|
|
|
## get a muxed stream for the passed connection
|
|
|
|
##
|
|
|
|
|
|
|
|
let muxer = c.selectMuxer(conn)
|
|
|
|
if not(isNil(muxer)):
|
|
|
|
return await muxer.newStream()
|
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
proc dropPeer*(c: ConnManager, peerId: PeerID) {.async.} =
|
2020-07-17 15:36:48 +00:00
|
|
|
## drop connections and cleanup resources for peer
|
|
|
|
##
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Dropping peer", peerId
|
2020-08-01 20:50:40 +00:00
|
|
|
let conns = c.conns.getOrDefault(peerId)
|
|
|
|
for conn in conns:
|
2020-09-15 20:19:22 +00:00
|
|
|
trace "Removing connection", conn
|
2020-08-01 20:50:40 +00:00
|
|
|
delConn(c, conn)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
var muxers: seq[MuxerHolder]
|
|
|
|
for conn in conns:
|
|
|
|
if conn in c.muxed:
|
|
|
|
muxers.add c.muxed[conn]
|
|
|
|
c.muxed.del(conn)
|
|
|
|
|
|
|
|
for muxer in muxers:
|
|
|
|
await closeMuxerHolder(muxer)
|
|
|
|
|
|
|
|
for conn in conns:
|
|
|
|
await conn.close()
|
2020-09-15 20:19:22 +00:00
|
|
|
trace "Dropped peer", peerId
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-11 19:20:14 +00:00
|
|
|
trace "Peer dropped", peerId
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
proc close*(c: ConnManager) {.async.} =
|
|
|
|
## cleanup resources for the connection
|
|
|
|
## manager
|
|
|
|
##
|
2020-09-09 17:12:08 +00:00
|
|
|
|
|
|
|
trace "Closing ConnManager"
|
2020-08-01 20:50:40 +00:00
|
|
|
let conns = c.conns
|
|
|
|
c.conns.clear()
|
|
|
|
|
|
|
|
let muxed = c.muxed
|
|
|
|
c.muxed.clear()
|
|
|
|
|
|
|
|
for _, muxer in muxed:
|
|
|
|
await closeMuxerHolder(muxer)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-08-01 20:50:40 +00:00
|
|
|
for _, conns2 in conns:
|
|
|
|
for conn in conns2:
|
|
|
|
await conn.close()
|
2020-09-09 17:12:08 +00:00
|
|
|
|
|
|
|
trace "Closed ConnManager"
|