Skip to content

Conversation

@u7512270
Copy link

Overview
This pull request introduces a new class, PeerTimeoutRegistry, to improve how the BT client handles peer timeouts and reconnections. Previously, the system used a simple timeoutedPeers.remove(disconnectedPeer) approach, which could allow misbehaving peers to reconnect immediately after timing out. This PR ensures that such peers remain banned for a defined duration, improving stability and fairness in peer management.

Problem Statement
The existing implementation in PeerRegistry and related classes did not enforce a consistent cooldown period for disconnected peers. As a result, misbehaving or unstable peers could rapidly reconnect, potentially overloading the client and degrading download performance. Additionally, timeout information was not persisted, meaning bans were lost after restarts—an issue particularly relevant for Android environments where the runtime may restart frequently.

Solution
This PR introduces the PeerTimeoutRegistry utility, which:
Tracks peers that have been temporarily banned after a timeout.

Enforces a fixed cooldown duration before allowing reconnection.

Periodically cleans up expired bans to keep memory usage low.

Supports saving and loading timeout data from a file for persistence across sessions.

The solution integrates seamlessly into existing logic and avoids modifying core BitTorrent message flows.

Key Features
Configurable ban duration via constructor (PeerTimeoutRegistry(long, TimeUnit)).
Thread-safe design using ConcurrentHashMap for concurrent access.
Persistence support with saveToFile() and loadFromFile() methods.
Automatic cleanup of expired bans to maintain efficiency.
Lightweight and modular, easily extensible for further peer management policies.

Testing
New Junit tests were added to verify correctness and robustness:
Ensures peers remain banned during the timeout period.
Confirms peers are re-allowed after expiry.
Validates persistence through file save/load operations.
Tests edge cases such as empty registries and cleanup correctness.
All tests pass locally, confirming expected behavior under different conditions.

Impact
This update:
Improves peer stability and fairness during torrent sessions.
Prevents abuse by limiting reconnect frequency.
Lays groundwork for more advanced peer reliability tracking.
Helps ensure better performance and reliability on Android devices where restarts are common.

Next Steps
Future improvements could include:
Integrating metrics for ban frequency to identify unreliable peers.
Configurable persistence paths for different environments.
Enhanced logging and user feedback for debugging.

Related Issue
Fixes: #228

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.

[QUESTION] Code affecting the performance

1 participant