Improve Peer Timeout Handling with Persistent Ban Registry #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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