Skip to content

Implement Advanced Connection Management Features from JS libp2p #553

@acul71

Description

@acul71

Description

Missing Features in Python's Connection Management Compared to JavaScript

After analyzing the JavaScript implementation https://github.com/libp2p/js-libp2p/blob/d53ef170cb171f5301758d5b2fc9e782950b4204/packages/libp2p/src/connection-manager/index.ts and comparing it with the Python codebase, here are the key features that are missing in the Python implementation:

Advanced Connection Management Features

  • Multiple connections per peer support (JavaScript uses PeerMap<Connection[]> while Python uses a simple dict[ID, INetConn])
  • Connection limits and resource management with configurable thresholds
  • Connection pruning mechanism to manage resource usage
  • Rate limiting for incoming connections

Connection Queue Management

  • Sophisticated dial queue with priority-based scheduling
  • Reconnection queue with configurable retry policies
  • Maximum queue length limits
  • Parallel dial limits

Connection Configuration Options

Extensive configuration options for timeouts:

  • dialTimeout
  • inboundUpgradeTimeout
  • outboundUpgradeTimeout
  • protocolNegotiationTimeout
  • outboundStreamProtocolNegotiationTimeout
  • inboundStreamProtocolNegotiationTimeout

Connection limits:

  • maxConnections
  • maxParallelDials
  • maxDialQueueLength
  • maxPeerAddrsToDial
  • maxIncomingPendingConnections

Security and Access Control

  • Allow/deny lists for IP addresses
  • Connection gating based on IP networks
  • Rate limiting for incoming connections per host

Metrics and Monitoring

Detailed metrics tracking for:

  • Connection counts (inbound/outbound)
  • Pending connections
  • Protocol streams per connection
  • 90th percentile metrics for streams

Reconnection Management

  • Automatic reconnection for peers tagged with KEEP_ALIVE
  • Configurable retry policies with backoff
  • Maximum parallel reconnects limit

Connection State Management

  • More granular connection state tracking
  • Better handling of connection lifecycle events
  • Improved error handling and connection cleanup

Address Management

  • Address sorting before dialing
  • DNS resolver support
  • Thin waist address handling

Event System

  • More comprehensive event system for connection state changes
  • Better event handling for connection lifecycle events

Resource Management

  • Better handling of pending connections
  • More sophisticated connection pruning
  • Resource limits enforcement

Motivation

The Python implementation is more basic and focuses on core functionality, while the JavaScript version provides a more complete and feature-rich connection management system. The JavaScript version also has better resource management, monitoring capabilities, and configuration options.

Current Implementation

This Python implementation of libp2p has similar connection management functionality to the JavaScript implementation

Swarm Class in py-libp2p: Key Components and Similarities

Core Connection Management

The Swarm class in libp2p/network/swarm.py serves as the main connection manager, similar to the JavaScript connection-manager.ts. It handles:

  • Opening connections (dial_peer, dial_addr)
  • Closing connections (close_peer)
  • Managing active connections (stored in self.connections dictionary)
  • Connection lifecycle management

Connection Querying

The Swarm class provides methods to:

  • Get active connections through self.connections
  • Check connection status
  • Filter connections by peer ID

Connection Management

The Swarm class handles:

  • Opening new connections via dial_peer and dial_addr
  • Closing connections via close_peer
  • Managing both direct peer ID and multiaddr-based connections

Connection Limits and Resources

The implementation includes:

  • Connection tracking through the connections dictionary
  • Connection cleanup and resource management
  • Connection state management through events

Additional Features

The SwarmConn class in libp2p/network/connection/swarm_connection.py provides:

  • Stream management
  • Connection state tracking
  • Event-based notifications for connection state changes

Integration Points

The Swarm class works with:

  • MultiaddrConnection for low-level connection handling
  • PeerId for peer identification
  • PeerStore for peer information management
  • Transport layer for actual connection establishment

The connection management is more tightly integrated with the Swarm class rather than being a separate connection manager
The Python implementation uses a simpler connection model where each peer ID maps to a single connection, while the JavaScript version allows multiple connections per peer ID

Are you planning to do it yourself in a pull request ?

Maybe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions