Skip to content

feat(p2p): Implement peer speed weighting for segment downloads#520

Open
mrlika wants to merge 7 commits intomainfrom
feature/p2p-peer-speed-weighting
Open

feat(p2p): Implement peer speed weighting for segment downloads#520
mrlika wants to merge 7 commits intomainfrom
feature/p2p-peer-speed-weighting

Conversation

@mrlika
Copy link
Copy Markdown
Member

@mrlika mrlika commented Apr 12, 2026

Description

This PR optimizes the P2P hybrid loader by implementing a peer speed weighting mechanism. Previously, when multiple peers had a requested segment, the peer was selected purely randomly. This update prioritizes faster peers based on their historical throughput to improve segment download efficiency and overall swarm health.

Changes

  • Bandwidth Calculation: Integrated BandwidthCalculator into the Peer class, which calculates and caches the downloadBandwidth to avoid excessive calculation overhead.
  • Weighted Selection: Replaced the pure random selector with a weighted random selector (Utils.getWeightedRandomItem) in loader.ts. Faster peers now have higher selection probabilities. New or untested peers are allocated a baseline throughput (or 10% of the max speed in the swarm) to ensure they continue to be organically benchmarked.

This commit modifies P2P peer selection to prioritize faster peers based on their historical throughput instead of random selection. It introduces `p2pPeerSpeedAveragingWindowMs` and `p2pPeerSpeedBaselineBps` configurations, integrates a bandwidth calculator in the `Peer` class, and uses a weighted random selection algorithm when multiple peers have a segment.
@mrlika mrlika requested a review from DimaDemchenko April 12, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant