mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-02 05:27:17 +00:00
Make peer discovery timeout configurable. Add milliseconds to error message if timeout kicks in.
This commit is contained in:
@@ -82,6 +82,8 @@ public class PeerGroup implements TransactionBroadcaster {
|
||||
public static final int DEFAULT_CONNECTIONS = 12;
|
||||
private static final int TOR_TIMEOUT_SECONDS = 60;
|
||||
private volatile int vMaxPeersToDiscoverCount = 100;
|
||||
private static final long DEFAULT_PEER_DISCOVERY_TIMEOUT_MILLIS = 5000;
|
||||
private volatile long vPeerDiscoveryTimeoutMillis = DEFAULT_PEER_DISCOVERY_TIMEOUT_MILLIS;
|
||||
|
||||
protected final ReentrantLock lock = Threading.lock("peergroup");
|
||||
|
||||
@@ -428,6 +430,13 @@ public class PeerGroup implements TransactionBroadcaster {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is how many milliseconds we wait for peer discoveries to return their results.
|
||||
*/
|
||||
public void setPeerDiscoveryTimeoutMillis(long peerDiscoveryTimeoutMillis) {
|
||||
this.vPeerDiscoveryTimeoutMillis = peerDiscoveryTimeoutMillis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the desired number of connections that we will create to peers. Note that if there are already peers
|
||||
* open and the new value is lower than the current number of peers, those connections will be terminated. Likewise
|
||||
@@ -869,11 +878,12 @@ public class PeerGroup implements TransactionBroadcaster {
|
||||
// Don't hold the lock whilst doing peer discovery: it can take a long time and cause high API latency.
|
||||
checkState(!lock.isHeldByCurrentThread());
|
||||
int maxPeersToDiscoverCount = this.vMaxPeersToDiscoverCount;
|
||||
long peerDiscoveryTimeoutMillis = this.vPeerDiscoveryTimeoutMillis;
|
||||
long start = System.currentTimeMillis();
|
||||
final List<PeerAddress> addressList = Lists.newLinkedList();
|
||||
for (PeerDiscovery peerDiscovery : peerDiscoverers /* COW */) {
|
||||
InetSocketAddress[] addresses;
|
||||
addresses = peerDiscovery.getPeers(requiredServices, 5, TimeUnit.SECONDS);
|
||||
addresses = peerDiscovery.getPeers(requiredServices, peerDiscoveryTimeoutMillis, TimeUnit.MILLISECONDS);
|
||||
for (InetSocketAddress address : addresses) addressList.add(new PeerAddress(address));
|
||||
if (addressList.size() >= maxPeersToDiscoverCount) break;
|
||||
}
|
||||
@@ -891,8 +901,8 @@ public class PeerGroup implements TransactionBroadcaster {
|
||||
});
|
||||
}
|
||||
}
|
||||
log.info("Peer discovery took {}msec and returned {} items",
|
||||
System.currentTimeMillis() - start, addressList.size());
|
||||
log.info("Peer discovery took {}ms and returned {} items", System.currentTimeMillis() - start,
|
||||
addressList.size());
|
||||
return addressList.size();
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +112,8 @@ public class MultiplexingDiscovery implements PeerDiscovery {
|
||||
Collections.addAll(addrs, inetAddresses);
|
||||
}
|
||||
if (addrs.size() == 0)
|
||||
throw new PeerDiscoveryException("No peer discovery returned any results: check internet connection?");
|
||||
throw new PeerDiscoveryException("No peer discovery returned any results in "
|
||||
+ timeoutUnit.toMillis(timeoutValue) + "ms. Check internet connection?");
|
||||
Collections.shuffle(addrs);
|
||||
vThreadPool.shutdownNow();
|
||||
return addrs.toArray(new InetSocketAddress[addrs.size()]);
|
||||
|
||||
Reference in New Issue
Block a user