forked from Qortal/qortal
Add method to prune old peers from database
This commit is contained in:
parent
bf8a12d422
commit
92e60e2531
@ -54,6 +54,10 @@ public class Network extends Thread {
|
|||||||
private static final int BROADCAST_INTERVAL = 60 * 1000; // ms
|
private static final int BROADCAST_INTERVAL = 60 * 1000; // ms
|
||||||
/** Maximum time since last successful connection for peer info to be propagated, in milliseconds. */
|
/** Maximum time since last successful connection for peer info to be propagated, in milliseconds. */
|
||||||
private static final long RECENT_CONNECTION_THRESHOLD = 24 * 60 * 60 * 1000; // ms
|
private static final long RECENT_CONNECTION_THRESHOLD = 24 * 60 * 60 * 1000; // ms
|
||||||
|
/** Maximum time since last connection attempt before a peer is potentially considered "old", in milliseconds. */
|
||||||
|
private static final long OLD_PEER_ATTEMPTED_PERIOD = 24 * 60 * 60 * 1000; // ms
|
||||||
|
/** Maximum time since last successful connection before a peer is potentially considered "old", in milliseconds. */
|
||||||
|
private static final long OLD_PEER_CONNECTION_PERIOD = 7 * 24 * 60 * 60 * 1000; // ms
|
||||||
|
|
||||||
private static final String[] INITIAL_PEERS = new String[] { "node1.qora.org", "node2.qora.org" };
|
private static final String[] INITIAL_PEERS = new String[] { "node1.qora.org", "node2.qora.org" };
|
||||||
|
|
||||||
@ -169,6 +173,8 @@ public class Network extends Thread {
|
|||||||
while (true) {
|
while (true) {
|
||||||
acceptConnections();
|
acceptConnections();
|
||||||
|
|
||||||
|
pruneOldPeers();
|
||||||
|
|
||||||
createConnection();
|
createConnection();
|
||||||
|
|
||||||
if (System.currentTimeMillis() >= this.nextBroadcast) {
|
if (System.currentTimeMillis() >= this.nextBroadcast) {
|
||||||
@ -234,13 +240,35 @@ public class Network extends Thread {
|
|||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createConnection() throws InterruptedException, DataException {
|
private void pruneOldPeers() throws InterruptedException, DataException {
|
||||||
/*
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
synchronized (this.connectedPeers) {
|
// Fetch all known peers
|
||||||
if (connectedPeers.size() >= minPeers)
|
List<PeerData> peers = repository.getNetworkRepository().getAllPeers();
|
||||||
return;
|
|
||||||
|
// "Old" peers:
|
||||||
|
// we have attempted to connect within the last day
|
||||||
|
// we last managed to connect over a week ago
|
||||||
|
final long now = NTP.getTime();
|
||||||
|
Predicate<PeerData> isNotOldPeer = peerData -> {
|
||||||
|
if (peerData.getLastAttempted() == null || peerData.getLastAttempted() > now - OLD_PEER_ATTEMPTED_PERIOD)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (peerData.getLastConnected() == null || peerData.getLastConnected() > now - OLD_PEER_CONNECTION_PERIOD)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
peers.removeIf(isNotOldPeer);
|
||||||
|
|
||||||
|
for (PeerData peerData : peers) {
|
||||||
|
LOGGER.debug(String.format("Deleting old peer %s from repository", peerData.getAddress().toString()));
|
||||||
|
repository.getNetworkRepository().delete(peerData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
private void createConnection() throws InterruptedException, DataException {
|
||||||
if (this.getOutboundHandshakeCompletedPeers().size() >= minPeers)
|
if (this.getOutboundHandshakeCompletedPeers().size() >= minPeers)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user