2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# 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-07-17 15:36:48 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
import std/[tables, sequtils, sets]
|
2021-05-25 17:06:04 +00:00
|
|
|
import pkg/[chronos, chronicles, metrics]
|
2020-07-17 15:36:48 +00:00
|
|
|
import peerinfo,
|
2021-09-08 09:07:46 +00:00
|
|
|
peerstore,
|
2020-07-17 15:36:48 +00:00
|
|
|
stream/connection,
|
2020-09-23 14:07:16 +00:00
|
|
|
muxers/muxer,
|
2021-01-21 04:00:24 +00:00
|
|
|
utils/semaphore,
|
2020-09-23 14:07:16 +00:00
|
|
|
errors
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-09-15 06:03:53 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p connmanager"
|
2020-09-15 06:03:53 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
declareGauge(libp2p_peers, "total connected peers")
|
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
const
|
2021-01-21 04:00:24 +00:00
|
|
|
MaxConnections* = 50
|
2022-01-04 11:21:24 +00:00
|
|
|
MaxConnectionsPerPeer* = 1
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
type
|
2021-05-21 16:27:01 +00:00
|
|
|
TooManyConnectionsError* = object of LPError
|
2023-02-21 16:49:41 +00:00
|
|
|
AlreadyExpectingConnectionError* = object of LPError
|
2021-01-21 04:00:24 +00:00
|
|
|
|
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* =
|
2021-09-08 09:07:46 +00:00
|
|
|
proc(peerId: PeerId, event: ConnEvent): Future[void]
|
2023-06-07 11:12:49 +00:00
|
|
|
{.gcsafe, raises: [].}
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2020-11-28 16:59:47 +00:00
|
|
|
PeerEventKind* {.pure.} = enum
|
2020-09-23 14:07:16 +00:00
|
|
|
Left,
|
|
|
|
Joined
|
|
|
|
|
2020-11-28 16:59:47 +00:00
|
|
|
PeerEvent* = object
|
|
|
|
case kind*: PeerEventKind
|
|
|
|
of PeerEventKind.Joined:
|
|
|
|
initiator*: bool
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2020-09-23 14:07:16 +00:00
|
|
|
PeerEventHandler* =
|
2023-06-07 11:12:49 +00:00
|
|
|
proc(peerId: PeerId, event: PeerEvent): Future[void] {.gcsafe, raises: [].}
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
ConnManager* = ref object of RootObj
|
2021-01-21 04:00:24 +00:00
|
|
|
maxConnsPerPeer: int
|
|
|
|
inSema*: AsyncSemaphore
|
|
|
|
outSema*: AsyncSemaphore
|
2023-03-08 11:30:19 +00:00
|
|
|
muxed: Table[PeerId, seq[Muxer]]
|
2021-05-19 06:49:55 +00:00
|
|
|
connEvents: array[ConnEventKind, OrderedSet[ConnEventHandler]]
|
|
|
|
peerEvents: array[PeerEventKind, OrderedSet[PeerEventHandler]]
|
2023-03-08 11:30:19 +00:00
|
|
|
expectedConnectionsOverLimit*: Table[(PeerId, Direction), Future[Muxer]]
|
2021-09-08 09:07:46 +00:00
|
|
|
peerStore*: PeerStore
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2022-08-03 14:48:19 +00:00
|
|
|
ConnectionSlot* = object
|
|
|
|
connManager: ConnManager
|
|
|
|
direction: Direction
|
|
|
|
|
2021-01-21 04:00:24 +00:00
|
|
|
proc newTooManyConnectionsError(): ref TooManyConnectionsError {.inline.} =
|
|
|
|
result = newException(TooManyConnectionsError, "Too many connections")
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc new*(C: type ConnManager,
|
2021-01-21 04:00:24 +00:00
|
|
|
maxConnsPerPeer = MaxConnectionsPerPeer,
|
|
|
|
maxConnections = MaxConnections,
|
|
|
|
maxIn = -1,
|
|
|
|
maxOut = -1): ConnManager =
|
|
|
|
var inSema, outSema: AsyncSemaphore
|
2021-01-28 03:27:33 +00:00
|
|
|
if maxIn > 0 or maxOut > 0:
|
2021-01-21 04:00:24 +00:00
|
|
|
inSema = newAsyncSemaphore(maxIn)
|
|
|
|
outSema = newAsyncSemaphore(maxOut)
|
|
|
|
elif maxConnections > 0:
|
|
|
|
inSema = newAsyncSemaphore(maxConnections)
|
|
|
|
outSema = inSema
|
|
|
|
else:
|
|
|
|
raiseAssert "Invalid connection counts!"
|
|
|
|
|
|
|
|
C(maxConnsPerPeer: maxConnsPerPeer,
|
|
|
|
inSema: inSema,
|
|
|
|
outSema: outSema)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc connCount*(c: ConnManager, peerId: PeerId): int =
|
2023-03-08 11:30:19 +00:00
|
|
|
c.muxed.getOrDefault(peerId).len
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
proc connectedPeers*(c: ConnManager, dir: Direction): seq[PeerId] =
|
|
|
|
var peers = newSeq[PeerId]()
|
2023-03-08 11:30:19 +00:00
|
|
|
for peerId, mux in c.muxed:
|
|
|
|
if mux.anyIt(it.connection.dir == dir):
|
2022-12-16 11:32:00 +00:00
|
|
|
peers.add(peerId)
|
|
|
|
return peers
|
|
|
|
|
2023-03-30 22:16:39 +00:00
|
|
|
proc getConnections*(c: ConnManager): Table[PeerId, seq[Muxer]] =
|
|
|
|
return c.muxed
|
|
|
|
|
2020-09-23 14:07:16 +00:00
|
|
|
proc addConnEventHandler*(c: ConnManager,
|
2020-11-28 16:59:47 +00:00
|
|
|
handler: ConnEventHandler,
|
|
|
|
kind: ConnEventKind) =
|
2020-09-23 14:07:16 +00:00
|
|
|
## Add peer event handler - handlers must not raise exceptions!
|
|
|
|
##
|
2023-05-18 08:24:17 +00:00
|
|
|
if isNil(handler): return
|
|
|
|
c.connEvents[kind].incl(handler)
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
proc removeConnEventHandler*(c: ConnManager,
|
2020-11-28 16:59:47 +00:00
|
|
|
handler: ConnEventHandler,
|
|
|
|
kind: ConnEventKind) =
|
2021-05-21 16:27:01 +00:00
|
|
|
c.connEvents[kind].excl(handler)
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2020-11-28 16:59:47 +00:00
|
|
|
proc triggerConnEvent*(c: ConnManager,
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId: PeerId,
|
2023-12-05 07:05:32 +00:00
|
|
|
event: ConnEvent) {.async.} =
|
2020-09-23 14:07:16 +00:00
|
|
|
try:
|
2021-09-08 09:07:46 +00:00
|
|
|
trace "About to trigger connection events", peer = peerId
|
2021-05-19 06:49:55 +00:00
|
|
|
if c.connEvents[event.kind].len() > 0:
|
2021-09-08 09:07:46 +00:00
|
|
|
trace "triggering connection events", peer = peerId, event = $event.kind
|
2020-09-23 14:07:16 +00:00
|
|
|
var connEvents: seq[Future[void]]
|
|
|
|
for h in c.connEvents[event.kind]:
|
2021-09-08 09:07:46 +00:00
|
|
|
connEvents.add(h(peerId, event))
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
checkFutures(await allFinished(connEvents))
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2021-01-21 04:00:24 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-23 14:07:16 +00:00
|
|
|
warn "Exception in triggerConnEvents",
|
2021-09-08 09:07:46 +00:00
|
|
|
msg = exc.msg, peer = peerId, event = $event
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
proc addPeerEventHandler*(c: ConnManager,
|
|
|
|
handler: PeerEventHandler,
|
2020-11-28 16:59:47 +00:00
|
|
|
kind: PeerEventKind) =
|
2020-09-23 14:07:16 +00:00
|
|
|
## Add peer event handler - handlers must not raise exceptions!
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(handler): return
|
2023-05-18 08:24:17 +00:00
|
|
|
c.peerEvents[kind].incl(handler)
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
proc removePeerEventHandler*(c: ConnManager,
|
|
|
|
handler: PeerEventHandler,
|
2020-11-28 16:59:47 +00:00
|
|
|
kind: PeerEventKind) =
|
2023-05-18 08:24:17 +00:00
|
|
|
c.peerEvents[kind].excl(handler)
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
proc triggerPeerEvents*(c: ConnManager,
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId: PeerId,
|
2023-12-05 07:05:32 +00:00
|
|
|
event: PeerEvent) {.async.} =
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
trace "About to trigger peer events", peer = peerId
|
2021-05-19 06:49:55 +00:00
|
|
|
if c.peerEvents[event.kind].len == 0:
|
2020-09-23 14:07:16 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-09-08 09:07:46 +00:00
|
|
|
trace "triggering peer events", peer = peerId, event = $event
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
var peerEvents: seq[Future[void]]
|
2020-11-28 16:59:47 +00:00
|
|
|
for h in c.peerEvents[event.kind]:
|
2021-09-08 09:07:46 +00:00
|
|
|
peerEvents.add(h(peerId, event))
|
2020-09-23 14:07:16 +00:00
|
|
|
|
|
|
|
checkFutures(await allFinished(peerEvents))
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc: # handlers should not raise!
|
2021-09-08 09:07:46 +00:00
|
|
|
warn "Exception in triggerPeerEvents", exc = exc.msg, peer = peerId
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc expectConnection*(c: ConnManager, p: PeerId, dir: Direction): Future[Muxer] {.async.} =
|
2023-01-23 22:28:39 +00:00
|
|
|
## Wait for a peer to connect to us. This will bypass the `MaxConnectionsPerPeer`
|
2023-02-21 16:49:41 +00:00
|
|
|
let key = (p, dir)
|
|
|
|
if key in c.expectedConnectionsOverLimit:
|
|
|
|
raise newException(AlreadyExpectingConnectionError, "Already expecting an incoming connection from that peer")
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let future = newFuture[Muxer]()
|
2023-02-21 16:49:41 +00:00
|
|
|
c.expectedConnectionsOverLimit[key] = future
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return await future
|
|
|
|
finally:
|
2023-02-21 16:49:41 +00:00
|
|
|
c.expectedConnectionsOverLimit.del(key)
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc contains*(c: ConnManager, peerId: PeerId): bool =
|
2023-03-08 11:30:19 +00:00
|
|
|
peerId in c.muxed
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
proc contains*(c: ConnManager, muxer: Muxer): bool =
|
|
|
|
## checks if a muxer is being tracked by the connection
|
|
|
|
## manager
|
|
|
|
##
|
|
|
|
|
|
|
|
if isNil(muxer):
|
2023-03-08 11:30:19 +00:00
|
|
|
return false
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
let conn = muxer.connection
|
2023-03-08 11:30:19 +00:00
|
|
|
return muxer in c.muxed.getOrDefault(conn.peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc closeMuxer(muxer: Muxer) {.async.} =
|
|
|
|
trace "Cleaning up muxer", m = muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
await muxer.close()
|
|
|
|
if not(isNil(muxer.handler)):
|
2021-01-21 04:00:24 +00:00
|
|
|
try:
|
2023-03-08 11:30:19 +00:00
|
|
|
await muxer.handler # TODO noraises?
|
2021-01-21 04:00:24 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception in close muxer handler", exc = exc.msg
|
2023-03-08 11:30:19 +00:00
|
|
|
trace "Cleaned up muxer", m = muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc muxCleanup(c: ConnManager, mux: Muxer) {.async.} =
|
2020-11-02 20:35:26 +00:00
|
|
|
try:
|
2023-03-08 11:30:19 +00:00
|
|
|
trace "Triggering disconnect events", mux
|
|
|
|
let peerId = mux.connection.peerId
|
2021-01-04 18:59:05 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxers = c.muxed.getOrDefault(peerId).filterIt(it != mux)
|
|
|
|
if muxers.len > 0:
|
|
|
|
c.muxed[peerId] = muxers
|
|
|
|
else:
|
|
|
|
c.muxed.del(peerId)
|
|
|
|
libp2p_peers.set(c.muxed.len.int64)
|
|
|
|
await c.triggerPeerEvents(peerId, PeerEvent(kind: PeerEventKind.Left))
|
2021-01-04 18:59:05 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
if not(c.peerStore.isNil):
|
|
|
|
c.peerStore.cleanup(peerId)
|
2020-11-02 20:35:26 +00:00
|
|
|
|
|
|
|
await c.triggerConnEvent(
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId, ConnEvent(kind: ConnEventKind.Disconnected))
|
2020-11-02 20:35:26 +00:00
|
|
|
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",
|
2023-03-08 11:30:19 +00:00
|
|
|
mux, msg = exc.msg
|
2020-11-02 20:35:26 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc onClose(c: ConnManager, mux: Muxer) {.async.} =
|
2020-07-17 15:36:48 +00:00
|
|
|
## connection close even handler
|
|
|
|
##
|
|
|
|
## triggers the connections resource cleanup
|
|
|
|
##
|
2020-09-04 16:30:45 +00:00
|
|
|
try:
|
2023-03-08 11:30:19 +00:00
|
|
|
await mux.connection.join()
|
|
|
|
trace "Connection closed, cleaning up", mux
|
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",
|
2023-03-08 11:30:19 +00:00
|
|
|
errMsg = exc.msg, mux
|
2020-11-02 20:35:26 +00:00
|
|
|
finally:
|
2023-03-08 11:30:19 +00:00
|
|
|
await c.muxCleanup(mux)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc selectMuxer*(c: ConnManager,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2023-03-08 11:30:19 +00:00
|
|
|
dir: Direction): Muxer =
|
2020-07-17 15:36:48 +00:00
|
|
|
## Select a connection for the provided peer and direction
|
|
|
|
##
|
|
|
|
let conns = toSeq(
|
2023-03-08 11:30:19 +00:00
|
|
|
c.muxed.getOrDefault(peerId))
|
|
|
|
.filterIt( it.connection.dir == dir )
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
if conns.len > 0:
|
|
|
|
return conns[0]
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc selectMuxer*(c: ConnManager, peerId: PeerId): Muxer =
|
2020-07-17 15:36:48 +00:00
|
|
|
## Select a connection for the provided giving priority
|
|
|
|
## to outgoing connections
|
|
|
|
##
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
var mux = c.selectMuxer(peerId, Direction.Out)
|
|
|
|
if isNil(mux):
|
|
|
|
mux = c.selectMuxer(peerId, Direction.In)
|
|
|
|
if isNil(mux):
|
2020-08-15 19:50:31 +00:00
|
|
|
trace "connection not found", peerId
|
2023-03-08 11:30:19 +00:00
|
|
|
return mux
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc storeMuxer*(c: ConnManager,
|
|
|
|
muxer: Muxer)
|
2023-06-07 11:12:49 +00:00
|
|
|
{.raises: [CatchableError].} =
|
2023-03-08 11:30:19 +00:00
|
|
|
## store the connection and muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
##
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
if isNil(muxer):
|
|
|
|
raise newException(LPError, "muxer cannot be nil")
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
if isNil(muxer.connection):
|
|
|
|
raise newException(LPError, "muxer's connection cannot be nil")
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
if muxer.connection.closed or muxer.connection.atEof:
|
2021-06-02 13:39:10 +00:00
|
|
|
raise newException(LPError, "Connection closed or EOF")
|
2020-12-03 01:24:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let
|
|
|
|
peerId = muxer.connection.peerId
|
|
|
|
dir = muxer.connection.dir
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
# we use getOrDefault in the if below instead of [] to avoid the KeyError
|
2023-03-08 11:30:19 +00:00
|
|
|
if c.muxed.getOrDefault(peerId).len > c.maxConnsPerPeer:
|
|
|
|
let key = (peerId, dir)
|
2023-02-21 16:49:41 +00:00
|
|
|
let expectedConn = c.expectedConnectionsOverLimit.getOrDefault(key)
|
|
|
|
if expectedConn != nil and not expectedConn.finished:
|
2023-03-08 11:30:19 +00:00
|
|
|
expectedConn.complete(muxer)
|
2023-02-21 16:49:41 +00:00
|
|
|
else:
|
|
|
|
debug "Too many connections for peer",
|
2023-03-08 11:30:19 +00:00
|
|
|
conns = c.muxed.getOrDefault(peerId).len
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-02-21 16:49:41 +00:00
|
|
|
raise newTooManyConnectionsError()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2024-03-06 05:49:45 +00:00
|
|
|
var newPeer = false
|
|
|
|
c.muxed.withValue(peerId, muxers):
|
|
|
|
doAssert muxers[].len > 0
|
|
|
|
doAssert muxer notin muxers[]
|
|
|
|
muxers[].add(muxer)
|
|
|
|
do:
|
|
|
|
c.muxed[peerId] = @[muxer]
|
|
|
|
newPeer = true
|
2023-03-08 11:30:19 +00:00
|
|
|
libp2p_peers.set(c.muxed.len.int64)
|
|
|
|
|
|
|
|
asyncSpawn c.triggerConnEvent(
|
|
|
|
peerId, ConnEvent(kind: ConnEventKind.Connected, incoming: dir == Direction.In))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
if newPeer:
|
|
|
|
asyncSpawn c.triggerPeerEvents(
|
|
|
|
peerId, PeerEvent(kind: PeerEventKind.Joined, initiator: dir == Direction.Out))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
asyncSpawn c.onClose(muxer)
|
|
|
|
|
|
|
|
trace "Stored muxer",
|
|
|
|
muxer, direction = $muxer.connection.dir, peers = c.muxed.len
|
2020-08-15 19:50:31 +00:00
|
|
|
|
2022-08-03 14:48:19 +00:00
|
|
|
proc getIncomingSlot*(c: ConnManager): Future[ConnectionSlot] {.async.} =
|
|
|
|
await c.inSema.acquire()
|
|
|
|
return ConnectionSlot(connManager: c, direction: In)
|
2021-01-21 04:00:24 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
proc getOutgoingSlot*(c: ConnManager, forceDial = false): ConnectionSlot {.raises: [TooManyConnectionsError].} =
|
2022-02-24 16:31:47 +00:00
|
|
|
if forceDial:
|
|
|
|
c.outSema.forceAcquire()
|
|
|
|
elif not c.outSema.tryAcquire():
|
2021-01-21 04:00:24 +00:00
|
|
|
trace "Too many outgoing connections!", count = c.outSema.count,
|
|
|
|
max = c.outSema.size
|
|
|
|
raise newTooManyConnectionsError()
|
2022-08-03 14:48:19 +00:00
|
|
|
return ConnectionSlot(connManager: c, direction: Out)
|
2021-01-21 04:00:24 +00:00
|
|
|
|
2023-01-24 16:04:42 +00:00
|
|
|
proc slotsAvailable*(c: ConnManager, dir: Direction): int =
|
|
|
|
case dir:
|
|
|
|
of Direction.In:
|
|
|
|
return c.inSema.count
|
|
|
|
of Direction.Out:
|
|
|
|
return c.outSema.count
|
|
|
|
|
2022-08-03 14:48:19 +00:00
|
|
|
proc release*(cs: ConnectionSlot) =
|
|
|
|
if cs.direction == In:
|
|
|
|
cs.connManager.inSema.release()
|
|
|
|
else:
|
|
|
|
cs.connManager.outSema.release()
|
2021-01-21 04:00:24 +00:00
|
|
|
|
2022-08-03 14:48:19 +00:00
|
|
|
proc trackConnection*(cs: ConnectionSlot, conn: Connection) =
|
|
|
|
if isNil(conn):
|
|
|
|
cs.release()
|
|
|
|
return
|
|
|
|
|
|
|
|
proc semaphoreMonitor() {.async.} =
|
|
|
|
try:
|
|
|
|
await conn.join()
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception in semaphore monitor, ignoring", exc = exc.msg
|
|
|
|
|
|
|
|
cs.release()
|
|
|
|
|
|
|
|
asyncSpawn semaphoreMonitor()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc trackMuxer*(cs: ConnectionSlot, mux: Muxer) =
|
|
|
|
if isNil(mux):
|
|
|
|
cs.release()
|
|
|
|
return
|
|
|
|
cs.trackConnection(mux.connection)
|
2020-11-02 20:35:26 +00:00
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
proc getStream*(c: ConnManager,
|
2023-12-05 07:05:32 +00:00
|
|
|
muxer: Muxer): Future[Connection] {.async.} =
|
2023-03-08 11:30:19 +00:00
|
|
|
## get a muxed stream for the passed muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
if not(isNil(muxer)):
|
|
|
|
return await muxer.newStream()
|
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
proc getStream*(c: ConnManager,
|
2023-12-05 07:05:32 +00:00
|
|
|
peerId: PeerId): Future[Connection] {.async.} =
|
2020-07-17 15:36:48 +00:00
|
|
|
## get a muxed stream for the passed peer from any connection
|
|
|
|
##
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
return await c.getStream(c.selectMuxer(peerId))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
proc getStream*(c: ConnManager,
|
2023-03-08 11:30:19 +00:00
|
|
|
peerId: PeerId,
|
2023-12-05 07:05:32 +00:00
|
|
|
dir: Direction): Future[Connection] {.async.} =
|
2023-03-08 11:30:19 +00:00
|
|
|
## get a muxed stream for the passed peer from a connection with `dir`
|
2020-07-17 15:36:48 +00:00
|
|
|
##
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
return await c.getStream(c.selectMuxer(peerId, dir))
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-12-16 10:05:20 +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
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxers = c.muxed.getOrDefault(peerId)
|
2020-08-01 20:50:40 +00:00
|
|
|
|
|
|
|
for muxer in muxers:
|
2023-03-08 11:30:19 +00:00
|
|
|
await closeMuxer(muxer)
|
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 muxed = c.muxed
|
|
|
|
c.muxed.clear()
|
|
|
|
|
2023-02-21 16:49:41 +00:00
|
|
|
let expected = c.expectedConnectionsOverLimit
|
|
|
|
c.expectedConnectionsOverLimit.clear()
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
for _, fut in expected:
|
|
|
|
await fut.cancelAndWait()
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
for _, muxers in muxed:
|
|
|
|
for mux in muxers:
|
|
|
|
await closeMuxer(mux)
|
2020-09-09 17:12:08 +00:00
|
|
|
|
|
|
|
trace "Closed ConnManager"
|
2022-12-16 11:32:00 +00:00
|
|
|
|