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,
|
|
|
|
muxers/muxer
|
|
|
|
|
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
|
|
|
|
|
|
|
|
MuxerHolder = object
|
|
|
|
muxer: Muxer
|
|
|
|
handle: Future[void]
|
|
|
|
|
|
|
|
ConnManager* = ref object of RootObj
|
|
|
|
# NOTE: don't change to PeerInfo here
|
|
|
|
# the reference semantics on the PeerInfo
|
|
|
|
# object itself make it succeptible to
|
|
|
|
# copies and mangling by unrelated code.
|
|
|
|
conns: Table[PeerID, HashSet[Connection]]
|
|
|
|
muxed: Table[Connection, MuxerHolder]
|
|
|
|
maxConns: int
|
|
|
|
|
|
|
|
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]())
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
# do not need to propogate 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-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
|
|
|
|
|
|
|
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:
|
|
|
|
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-09 17:12:08 +00:00
|
|
|
|
2020-09-08 06:24:28 +00:00
|
|
|
trace "Dropped peer", 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"
|