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
|
|
|
|
|
|
|
|
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.} =
|
|
|
|
trace "cleaning up muxer for peer"
|
|
|
|
|
|
|
|
await muxerHolder.muxer.close()
|
|
|
|
if not(isNil(muxerHolder.handle)):
|
|
|
|
await muxerHolder.handle # TODO noraises?
|
|
|
|
|
|
|
|
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-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-08-01 20:50:40 +00:00
|
|
|
trace "connection cleaned up", peer = $conn.peerInfo
|
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-08-12 00:05:49 +00:00
|
|
|
await conn.join()
|
2020-07-17 15:36:48 +00:00
|
|
|
trace "triggering connection cleanup"
|
|
|
|
await c.cleanupConn(conn)
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
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:
|
|
|
|
trace "too many connections", peer = $peerId,
|
2020-07-17 15:36:48 +00:00
|
|
|
conns = c.conns
|
2020-08-01 20:50:40 +00:00
|
|
|
.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
|
|
|
|
|
|
|
# launch on close listener
|
|
|
|
asyncCheck c.onClose(conn)
|
|
|
|
libp2p_peers.set(c.conns.len.int64)
|
|
|
|
|
|
|
|
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-08-01 20:50:40 +00:00
|
|
|
trace "stored connection", 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-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-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
proc close*(c: ConnManager) {.async.} =
|
|
|
|
## cleanup resources for the connection
|
|
|
|
## manager
|
|
|
|
##
|
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()
|