diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java index 1613bff5..04be4b7b 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java +++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java @@ -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 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(); } diff --git a/core/src/main/java/org/bitcoinj/net/discovery/MultiplexingDiscovery.java b/core/src/main/java/org/bitcoinj/net/discovery/MultiplexingDiscovery.java index e3bb733b..fbd00395 100644 --- a/core/src/main/java/org/bitcoinj/net/discovery/MultiplexingDiscovery.java +++ b/core/src/main/java/org/bitcoinj/net/discovery/MultiplexingDiscovery.java @@ -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()]);