diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 7df2a4a1..41c33811 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -342,7 +342,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac do { try { connectToAnyPeer(); - } catch(PeerDiscoveryException e) { + } catch (PeerDiscoveryException e) { groupBackoff.trackFailure(); } } while (isRunning() && countConnectedAndPendingPeers() < getMaxConnections()); @@ -575,6 +575,8 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac } protected void discoverPeers() throws PeerDiscoveryException { + if (peerDiscoverers.isEmpty()) + throw new PeerDiscoveryException("No peer discoverers registered"); long start = System.currentTimeMillis(); Set addressSet = Sets.newHashSet(); for (PeerDiscovery peerDiscovery : peerDiscoverers) { @@ -630,10 +632,10 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac final State state = state(); if (!(state == State.STARTING || state == State.RUNNING)) return; - final PeerAddress addr; + PeerAddress addr = null; long nowMillis = Utils.currentTimeMillis(); - + long retryTime = 0; lock.lock(); try { if (!haveReadyInactivePeer(nowMillis)) { @@ -646,18 +648,21 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac return; } addr = inactives.poll(); + retryTime = backoffMap.get(addr).getRetryTime(); } finally { + // discoverPeers might throw an exception if something goes wrong: we then hit this path with addr == null. + retryTime = Math.max(retryTime, groupBackoff.getRetryTime()); lock.unlock(); - } - - // Delay if any backoff is required - long retryTime = Math.max(backoffMap.get(addr).getRetryTime(), groupBackoff.getRetryTime()); - if (retryTime > nowMillis) { - // Sleep until retry time - Utils.sleep(retryTime - nowMillis); + if (retryTime > nowMillis) { + // Sleep until retry time + final long millis = retryTime - nowMillis; + log.info("Waiting {} msec before next connect attempt", millis, addr == null ? "" : " to " + addr); + Utils.sleep(millis); + } } // This method constructs a Peer and puts it into pendingPeers. + checkNotNull(addr); // Help static analysis which can't see that addr is always set if we didn't throw above. connectTo(addr, false); }