libp2p/connmanager

Search:
Group by:
Source   Edit  

Types

ConnectionLimits = object
Configuration for connection limits. Construct via ConnectionLimits.maxTotal for a single shared cap, or ConnectionLimits.maxInOut for independent inbound/outbound caps. Source   Edit  
ConnectionSlot = object
Source   Edit  
ConnEvent = object
  case kind*: ConnEventKind
  of ConnEventKind.Connected:
    incoming*: bool
  else:
    nil
Source   Edit  
ConnEventHandler = proc (peerId: PeerId; event: ConnEvent): Future[void] {.
    ...gcsafe, async: (...raises: [CancelledError]).}
Source   Edit  
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.
Source   Edit  
ConnManager = ref object of RootObj
  expectedConnectionsOverLimit*: Table[(PeerId, Direction), Future[Muxer]]
  peerStore*: PeerStore
Source   Edit  
DecayFn = proc (value: int; elapsed: Duration): int {....gcsafe, raises: [].}
Decay function applied to an ephemeral tag on each tick interval. elapsed is the time since the tag's last tick, for time-proportional decay. Returns the new value; returning ≤0 removes the tag. Source   Edit  
PeerEvent = object
  case kind*: PeerEventKind
  of PeerEventKind.Joined, PeerEventKind.Identified:
    initiator*: bool
  else:
    nil
Source   Edit  
PeerEventHandler = proc (peerId: PeerId; event: PeerEvent): Future[void] {.
    ...gcsafe, async: (...raises: [CancelledError]).}
Source   Edit  
PeerEventKind {.pure.} = enum
  Left, Joined, Identified
Source   Edit  
PeerScoring = object
  outboundBonus*: int = 100  ## `outboundBonus` is added to the score of every peer with an outbound connection
  decayResolution*: Duration = (value: 60000000000) ## `decayResolution` controls how often ephemeral tag decay functions are applied
                                                    ## It is the shortest interval at which any decaying tag can decay
Configuration for peer scoring parameters. Source   Edit  
WatermarkPolicy = object
  lowWater*: int             ## target peer count after trimming
  highWater*: int            ## peer count that triggers a trim cycle
  gracePeriod*: Duration     ## newly connected peers are exempt from trimming
  silencePeriod*: Duration   ## minimum interval between trim cycles
Configuration for hi/lo watermark connection management. When peer count exceeds highWater, a trim cycle runs until peer count drops to lowWater. Source   Edit  

Procs

proc addConnEventHandler(c: ConnManager; handler: ConnEventHandler;
                         kind: ConnEventKind) {....raises: [], tags: [],
    forbids: [].}
Add peer event handler - handlers must not raise exceptions! Source   Edit  
proc addPeerEventHandler(c: ConnManager; handler: PeerEventHandler;
                         kind: PeerEventKind) {....raises: [], tags: [],
    forbids: [].}
Add peer event handler - handlers must not raise exceptions! Source   Edit  
proc availableSlots(c: ConnManager; dir: Direction): int {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc bumpDecayingTag(c: ConnManager; peerId: PeerId; tag: string; delta: int) {.
    ...raises: [], tags: [], forbids: [].}
Add delta to an existing decaying tag value. Clamps to 0 from below. Source   Edit  
proc close(c: ConnManager): InternalRaisesFuture[void, (CancelledError,)] {.
    ...stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
Cleanup resources for the connection manager. Source   Edit  
proc connCount(c: ConnManager; peerId: PeerId): int {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc connectedPeers(c: ConnManager; dir: Direction): seq[PeerId] {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc contains(c: ConnManager; muxer: Muxer): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc contains(c: ConnManager; peerId: PeerId): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc decayFixed(amount: int): DecayFn {....raises: [], tags: [], forbids: [].}
Returns a DecayFn that subtracts amount from the tag value on each tick. The tag is removed when the value drops to 0 or below. Source   Edit  
proc decayLinear(coef: float): DecayFn {....raises: [], tags: [], forbids: [].}
Returns a DecayFn that multiplies the tag value by coef on each tick. The tag is removed when the value drops to 0 or below. Example: decayLinear(0.9) reduces by 10% per interval. Source   Edit  
proc decayNone(): DecayFn {....raises: [], tags: [], forbids: [].}
Returns a DecayFn that never decays the value (permanent ephemeral tag). Source   Edit  
proc dropPeer(c: ConnManager; peerId: PeerId): InternalRaisesFuture[void,
    (CancelledError,)] {....stackTrace: false, raises: [], gcsafe, raises: [],
                         tags: [RootEffect], forbids: [].}
drop connections and cleanup resources for peer Source   Edit  
proc expectConnection(c: ConnManager; p: PeerId; dir: Direction): InternalRaisesFuture[
    Muxer, (AlreadyExpectingConnectionError, CancelledError)] {.
    ...stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
Wait for a peer to connect to us. This will bypass the MaxConnectionsPerPeer Source   Edit  
proc getConnections(c: ConnManager): Table[PeerId, seq[Muxer]] {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc getIncomingSlot(c: ConnManager): InternalRaisesFuture[ConnectionSlot,
    (CancelledError,)] {....stackTrace: false, raises: [], gcsafe, raises: [],
                         tags: [RootEffect], forbids: [].}
Source   Edit  
proc getOutgoingSlot(c: ConnManager; forceDial = false): ConnectionSlot {.
    ...raises: [TooManyConnectionsError], raises: [], tags: [], forbids: [].}
Source   Edit  
proc getStream(c: ConnManager; muxer: Muxer): InternalRaisesFuture[MuxedStream,
    (LPStreamError, MuxerError, CancelledError)] {....stackTrace: false,
    raises: [], gcsafe, raises: [], tags: [RootEffect], forbids: [].}
get a muxed stream for the passed muxer Source   Edit  
proc getStream(c: ConnManager; peerId: PeerId): InternalRaisesFuture[
    MuxedStream, (LPStreamError, MuxerError, CancelledError)] {.
    ...stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
get a muxed stream for the passed peer from any connection Source   Edit  
proc getStream(c: ConnManager; peerId: PeerId; dir: Direction): InternalRaisesFuture[
    MuxedStream, (LPStreamError, MuxerError, CancelledError)] {.
    ...stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
get a muxed stream for the passed peer from a connection with dir Source   Edit  
proc isProtected(c: ConnManager; peerId: PeerId): bool {....raises: [], tags: [],
    forbids: [].}
Returns true if peerId has at least one active protection tag. Source   Edit  
proc maxConnections(c: ConnManager; dir: Direction): int {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc maxInOut(T: type ConnectionLimits; maxIn: int; maxOut: int): ConnectionLimits {.
    ...raises: [].}
Constructs ConnectionLimits with independent inbound/outbound caps. Source   Edit  
proc maxTotal(T: type ConnectionLimits; maxConnections: int): ConnectionLimits {.
    ...raises: [].}
Constructs ConnectionLimits with single shared cap limit. Source   Edit  
proc new(T: type ConnManager; maxConnsPerPeer: int = 0;
         limits: Opt[ConnectionLimits] = Opt.none(ConnectionLimits);
         watermark: Opt[WatermarkPolicy] = Opt.none(WatermarkPolicy);
         scoring: PeerScoring = PeerScoring()): ConnManager {....raises: [].}

Creates a ConnManager.

maxConnsPerPeer accepts values ≤0 to mean "use the default value".

limits selects the connection-cap strategy: ConnectionLimits.maxTotal(n) for a single shared cap, or ConnectionLimits.maxInOut(i, o) for independent per-direction caps. When omitted, the total cap defaults to DefaultMaxConnections.

When watermark is provided without limits the semaphore is omitted and hi/lo trimming is the only guard. When both are provided the semaphore blocks new connections hard while trimming also prunes existing ones.

Source   Edit  
proc peerScore(c: ConnManager; peerId: PeerId): int {....raises: [], tags: [],
    forbids: [].}
Returns the total score for peerId: outboundBonus (if peer has an outbound connection)
  • sum of all static tag values
  • sum of all current decaying tag values
Source   Edit  
proc protect(c: ConnManager; peerId: PeerId; tag: string) {....raises: [],
    tags: [], forbids: [].}
Mark peerId as protected from watermark trimming under tag. A peer remains protected as long as at least one tag is active. Source   Edit  
proc release(cs: ConnectionSlot) {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc removeConnEventHandler(c: ConnManager; handler: ConnEventHandler;
                            kind: ConnEventKind) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc removeDecayingTag(c: ConnManager; peerId: PeerId; tag: string) {.
    ...raises: [], tags: [], forbids: [].}
Immediately remove a decaying tag from peerId. Source   Edit  
proc removePeerEventHandler(c: ConnManager; handler: PeerEventHandler;
                            kind: PeerEventKind) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc selectMuxer(c: ConnManager; peerId: PeerId): Muxer {....raises: [], tags: [],
    forbids: [].}
Select a connection for the provided peer, giving priority to outgoing connections. Source   Edit  
proc selectMuxer(c: ConnManager; peerId: PeerId; dir: Direction): Muxer {.
    ...raises: [], tags: [], forbids: [].}
Select a connection for the provided peer and direction Source   Edit  
proc storeMuxer(c: ConnManager; muxer: Muxer): InternalRaisesFuture[void,
    (CancelledError, LPError)] {....stackTrace: false, raises: [], gcsafe,
                                 raises: [], tags: [RootEffect], forbids: [].}
store the connection and muxer Source   Edit  
proc tagPeer(c: ConnManager; peerId: PeerId; tag: string; value: int) {.
    ...raises: [], tags: [], forbids: [].}
Set a static tag on peerId. Tags accumulate additively into the peer score. Source   Edit  
proc tagPeerDecaying(c: ConnManager; peerId: PeerId; tag: string; value: int;
                     interval: Duration; decayFn: DecayFn) {....raises: [],
    tags: [RootEffect], forbids: [].}
Attach an ephemeral tag to peerId with an initial value. A single decay loop runs every decayResolution. On each run it applies decayFn to every tag whose interval has elapsed. A tag therefore decays at most once per decayResolution, even with a shorter interval. When the value drops to ≤0 the tag is removed automatically. Source   Edit  
proc trackConnection(cs: ConnectionSlot; conn: RawConn) {....raises: [],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc trackMuxer(cs: ConnectionSlot; mux: Muxer) {....raises: [],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc triggerConnEvent(c: ConnManager; peerId: PeerId; event: ConnEvent): InternalRaisesFuture[
    void, (CancelledError,)] {....stackTrace: false, raises: [], gcsafe,
                               raises: [], tags: [RootEffect], forbids: [].}
Source   Edit  
proc triggerPeerEvents(c: ConnManager; peerId: PeerId; event: PeerEvent): InternalRaisesFuture[
    void, (CancelledError,)] {....stackTrace: false, raises: [], gcsafe,
                               raises: [], tags: [RootEffect], forbids: [].}
Source   Edit  
proc unprotect(c: ConnManager; peerId: PeerId; tag: string): bool {....raises: [],
    tags: [], forbids: [].}
Remove tag protection from peerId. Returns true if the peer is still protected via another tag, false otherwise. Source   Edit  
proc untagPeer(c: ConnManager; peerId: PeerId; tag: string) {....raises: [],
    tags: [], forbids: [].}
Remove a static tag from peerId. Source   Edit  
proc waitForPeerReady(c: ConnManager; peerId: PeerId; timeout = 5.seconds): InternalRaisesFuture[
    bool, (CancelledError,)] {....stackTrace: false, raises: [], gcsafe,
                               raises: [], tags: [RootEffect], forbids: [].}
Wait until storeMuxer has emitted the Connected conn event for peerId. Existing ready peers bypass waiting. Source   Edit