Skip to content

Commit

Permalink
fix: update docs (#1520)
Browse files Browse the repository at this point in the history
Simplify exported types and publish API docs on release
  • Loading branch information
achingbrain committed Dec 20, 2022
1 parent 19e96cc commit 3e53c19
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 194 deletions.
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Expand Up @@ -857,8 +857,8 @@ The NAT manager can be configured as follows:
const node = await createLibp2p({
config: {
nat: {
description: 'my-node', // set as the port mapping description on the router, defaults the current libp2p version and your peer id
enabled: true, // defaults to true
description: 'my-node', // set as the port mapping description on the router, defaults the current libp2p version and your peer id
gateway: '192.168.1.1', // leave unset to auto-discover
externalIp: '80.1.1.1', // leave unset to auto-discover
localAddress: '129.168.1.123', // leave unset to auto-discover
Expand Down
8 changes: 8 additions & 0 deletions src/address-manager/index.ts
Expand Up @@ -7,6 +7,10 @@ import type { PeerId } from '@libp2p/interface-peer-id'
import type { TransportManager } from '@libp2p/interface-transport'

export interface AddressManagerInit {
/**
* Pass an function in this field to override the list of addresses
* that are announced to the network
*/
announceFilter?: AddressFilter

/**
Expand All @@ -30,6 +34,10 @@ export interface DefaultAddressManagerComponents {
transportManager: TransportManager
}

/**
* A function that takes a list of multiaddrs and returns a list
* to announce
*/
export interface AddressFilter {
(addrs: Multiaddr[]): Multiaddr[]
}
Expand Down
14 changes: 13 additions & 1 deletion src/circuit/index.ts
Expand Up @@ -12,12 +12,24 @@ import {
} from './constants.js'
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
import type { Startable } from '@libp2p/interfaces/startable'
import type { RelayConfig } from '../index.js'
import type { ContentRouting } from '@libp2p/interface-content-routing'
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
import type { TransportManager } from '@libp2p/interface-transport'
import type { PeerId } from '@libp2p/interface-peer-id'

export interface RelayConfig {
enabled: boolean
advertise: RelayAdvertiseConfig
hop: HopConfig
autoRelay: AutoRelayConfig
}

export interface HopConfig {
enabled?: boolean
active?: boolean
timeout: number
}

const log = logger('libp2p:relay')

export interface RelayAdvertiseConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/transport.ts
Expand Up @@ -17,7 +17,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interface-transport'
import type { Connection } from '@libp2p/interface-connection'
import type { RelayConfig } from '../index.js'
import type { RelayConfig } from './index.js'
import { abortableDuplex } from 'abortable-iterator'
import { TimeoutController } from 'timeout-abort-controller'
import { setMaxListeners } from 'events'
Expand Down
13 changes: 11 additions & 2 deletions src/config.ts
Expand Up @@ -31,7 +31,15 @@ const DefaultConfig: Partial<Libp2pInit> = {
resolvers: {
dnsaddr: dnsaddrResolver
},
addressSorter: publicAddressesFirst
addressSorter: publicAddressesFirst,
maxEventLoopDelay: Infinity,
pollInterval: Infinity,
maxAddrsToDial: Infinity,
startupReconnectTimeout: Infinity,
allow: [],
deny: [],
maxIncomingPendingConnections: Infinity,
inboundConnectionThreshold: Infinity
},
connectionGater: {},
transportManager: {
Expand All @@ -42,7 +50,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
enabled: true,
interval: 6e5,
bootDelay: 10e3
}
},
routers: []
},
nat: {
enabled: true,
Expand Down
105 changes: 102 additions & 3 deletions src/connection-manager/index.ts
Expand Up @@ -11,19 +11,118 @@ import { setMaxListeners } from 'events'
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
import type { ConnectionManager, ConnectionManagerEvents, Dialer } from '@libp2p/interface-connection-manager'
import * as STATUS from '@libp2p/interface-connection/status'
import type { PeerStore } from '@libp2p/interface-peer-store'
import { isMultiaddr, multiaddr, Multiaddr } from '@multiformats/multiaddr'
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
import { isMultiaddr, multiaddr, Multiaddr, Resolver } from '@multiformats/multiaddr'
import { PeerMap } from '@libp2p/peer-collections'
import { TimeoutController } from 'timeout-abort-controller'
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags'
import { RateLimiterMemory } from 'rate-limiter-flexible'
import type { Metrics } from '@libp2p/interface-metrics'
import type { Upgrader } from '@libp2p/interface-transport'
import { getPeer } from '../get-peer.js'
import type { ConnectionManagerConfig } from '../index.js'

const log = logger('libp2p:connection-manager')

export interface ConnectionManagerConfig {
/**
* The maximum number of connections libp2p is willing to have before it starts disconnecting. Defaults to `Infinity`
*/
maxConnections: number

/**
* The minimum number of connections below which libp2p not activate preemptive disconnections. Defaults to `0`.
*/
minConnections: number

/**
* Sets the maximum event loop delay (measured in milliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
*/
maxEventLoopDelay?: number

/**
* Sets the poll interval (in milliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
*/
pollInterval?: number

/**
* If true, try to connect to all discovered peers up to the connection manager limit
*/
autoDial?: boolean

/**
* How long to wait between attempting to keep our number of concurrent connections
* above minConnections
*/
autoDialInterval: number

/**
* Sort the known addresses of a peer before trying to dial
*/
addressSorter?: AddressSorter

/**
* Number of max concurrent dials
*/
maxParallelDials?: number

/**
* Number of max addresses to dial for a given peer
*/
maxAddrsToDial?: number

/**
* How long a dial attempt is allowed to take, including DNS resolution
* of the multiaddr, opening a socket and upgrading it to a Connection.
*/
dialTimeout?: number

/**
* When a new inbound connection is opened, the upgrade process (e.g. protect,
* encrypt, multiplex etc) must complete within this number of ms.
*/
inboundUpgradeTimeout: number

/**
* Number of max concurrent dials per peer
*/
maxDialsPerPeer?: number

/**
* Multiaddr resolvers to use when dialing
*/
resolvers?: Record<string, Resolver>

/**
* On startup we try to dial any peer that has previously been
* tagged with KEEP_ALIVE up to this timeout in ms. (default: 60000)
*/
startupReconnectTimeout?: number

/**
* A list of multiaddrs that will always be allowed (except if they are in the
* deny list) to open connections to this node even if we've reached maxConnections
*/
allow?: string[]

/**
* A list of multiaddrs that will never be allowed to open connections to
* this node under any circumstances
*/
deny?: string[]

/**
* If more than this many connections are opened per second by a single
* host, reject subsequent connections
*/
inboundConnectionThreshold?: number

/**
* The maximum number of parallel incoming connections allowed that have yet to
* complete the connection upgrade - e.g. choosing connection encryption, muxer, etc
*/
maxIncomingPendingConnections?: number
}

const defaultOptions: Partial<ConnectionManagerConfig> = {
maxConnections: Infinity,
minConnections: 0,
Expand Down

0 comments on commit 3e53c19

Please sign in to comment.