switch to map
This commit is contained in:
@@ -7,8 +7,8 @@ class HostPool {
|
|||||||
final def maxFailures
|
final def maxFailures
|
||||||
final def maxAge
|
final def maxAge
|
||||||
|
|
||||||
def verified = new HashSet()
|
def verified = new HashMap()
|
||||||
def unverified = new HashSet()
|
def unverified = new HashMap()
|
||||||
|
|
||||||
HostPool(maxFailures, maxAge) {
|
HostPool(maxFailures, maxAge) {
|
||||||
this.maxAge = maxAge
|
this.maxAge = maxAge
|
||||||
@@ -19,15 +19,15 @@ class HostPool {
|
|||||||
if (verified.isEmpty()) {
|
if (verified.isEmpty()) {
|
||||||
return Collections.emptyList()
|
return Collections.emptyList()
|
||||||
}
|
}
|
||||||
def asList = verified.stream().filter({ it -> leaf ? it.leafSlots : it.peerSlots}).collect(Collectors.toList())
|
def asList = verified.values().stream().filter({ it -> leaf ? it.leafSlots : it.peerSlots}).collect(Collectors.toList())
|
||||||
Collections.shuffle(asList)
|
Collections.shuffle(asList)
|
||||||
|
|
||||||
return asList[0..Math.min(max, asList.size()) -1]
|
return asList[0..Math.min(max, asList.size()) -1]
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized def addUnverified(host) {
|
synchronized def addUnverified(host) {
|
||||||
if (!verified.contains(host)) {
|
if (!verified.containsKey(host.destination)) {
|
||||||
unverified.add(host)
|
unverified.put(host.destination, host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,37 +35,39 @@ class HostPool {
|
|||||||
if (unverified.isEmpty()) {
|
if (unverified.isEmpty()) {
|
||||||
return Collections.emptyList()
|
return Collections.emptyList()
|
||||||
}
|
}
|
||||||
def asList = unverified.asList()
|
def asList = unverified.values().asList()
|
||||||
Collections.shuffle(asList)
|
Collections.shuffle(asList)
|
||||||
return asList[0..(Math.min(max, asList.size())-1)]
|
return asList[0..(Math.min(max, asList.size())-1)]
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized def verify(host) {
|
synchronized def verify(host) {
|
||||||
if (!unverified.remove(host))
|
if (!unverified.remove(host.destination))
|
||||||
throw new IllegalArgumentException()
|
throw new IllegalArgumentException()
|
||||||
host.verifyTime = System.currentTimeMillis();
|
host.verifyTime = System.currentTimeMillis();
|
||||||
host.verificationFailures = 0
|
host.verificationFailures = 0
|
||||||
verified.add(host)
|
verified.put(host.destination, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized def fail(host) {
|
synchronized def fail(host) {
|
||||||
if (!unverified.contains(host))
|
if (!unverified.containsKey(host.destination))
|
||||||
throw new IllegalArgumentException()
|
throw new IllegalArgumentException()
|
||||||
host.verificationFailures++
|
host.verificationFailures++
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized def age() {
|
synchronized def age() {
|
||||||
final long now = System.currentTimeMillis()
|
final long now = System.currentTimeMillis()
|
||||||
for (Iterator iter = verified.iterator(); iter.hasNext();) {
|
for (Iterator iter = verified.keySet().iterator(); iter.hasNext();) {
|
||||||
def host = iter.next()
|
def destination = iter.next()
|
||||||
|
def host = verified.get(destination)
|
||||||
if (host.verifyTime + maxAge < now) {
|
if (host.verifyTime + maxAge < now) {
|
||||||
iter.remove()
|
iter.remove()
|
||||||
unverified.add(host)
|
unverified.put(host.destination, host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Iterator iter = unverified.iterator(); iter.hasNext();) {
|
for (Iterator iter = unverified.keySet().iterator(); iter.hasNext();) {
|
||||||
def host = iter.next()
|
def destination = iter.next()
|
||||||
|
def host = unverified.get(destination)
|
||||||
if (host.verificationFailures >= maxFailures) {
|
if (host.verificationFailures >= maxFailures) {
|
||||||
iter.remove()
|
iter.remove()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user