diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java index 19afd52640cd..d043ea27aa3e 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java +++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java @@ -498,7 +498,8 @@ public void go() { if (retryTime > now) { long delay = retryTime - now; log.info("Waiting {} ms before next connect attempt {}", delay, addrToTry == null ? "" : "to " + addrToTry); - inactives.add(addrToTry); + if (!isAlreadyAdded(addrToTry)) + inactives.add(addrToTry); executor.schedule(this, delay, TimeUnit.MILLISECONDS); return; } @@ -512,6 +513,17 @@ public void go() { } }; + private boolean isAlreadyAdded(PeerAddress peerAddress) { + boolean isAlreadyAdded = false; + for (PeerAddress a : inactives) { + if (a.getHostname() != null && a.getHostname().equals(peerAddress.getHostname())) { + isAlreadyAdded = true; + break; + } + } + return isAlreadyAdded; + } + private void triggerConnections() { // Run on a background thread due to the need to potentially retry and back off in the background. if (!executor.isShutdown()) @@ -873,7 +885,10 @@ private boolean addInactive(PeerAddress peerAddress) { return false; } backoffMap.put(peerAddress, new ExponentialBackoff(peerBackoffParams)); - inactives.offer(peerAddress); + + if (!isAlreadyAdded(peerAddress)) + inactives.offer(peerAddress); + return true; } finally { lock.unlock();