track last successful attempt. Only re-attempt hosts if they have ever been successful. Do not serialize hosts considered hopeless

pull/24/head
Zlatin Balevsky 2019-09-29 16:19:19 +01:00
parent 549e8c2d98
commit 1d97374857
2 changed files with 14 additions and 3 deletions

View File

@ -10,6 +10,7 @@ class Host {
private final int clearInterval
int failures,successes
long lastAttempt
long lastSuccessfulAttempt
public Host(Destination destination, int clearInterval) {
this.destination = destination
@ -20,6 +21,7 @@ class Host {
failures = 0
successes++
lastAttempt = System.currentTimeMillis()
lastSuccessfulAttempt = lastAttempt
}
synchronized void onFailure() {
@ -41,6 +43,12 @@ class Host {
}
synchronized void canTryAgain() {
System.currentTimeMillis() - lastAttempt > (clearInterval * 60 * 1000)
lastSuccessfulAttempt > 0 &&
System.currentTimeMillis() - lastAttempt > (clearInterval * 60 * 1000)
}
synchronized void isHopeless() {
isFailed() &&
System.currentTimeMillis() - lastSuccessfulAttempt > (clearInterval * 24 * 60 * 1000)
}
}

View File

@ -111,6 +111,8 @@ class HostCache extends Service {
host.successes = Integer.valueOf(String.valueOf(entry.successes))
if (entry.lastAttempt != null)
host.lastAttempt = entry.lastAttempt
if (entry.lastSuccessfulAttempt != null)
host.lastSuccessfulAttempt = entry.lastSuccessfulAttempt
if (allowHost(host))
hosts.put(dest, host)
}
@ -140,12 +142,13 @@ class HostCache extends Service {
storage.delete()
storage.withPrintWriter { writer ->
hosts.each { dest, host ->
if (allowHost(host)) {
if (allowHost(host) && !host.isHopeless()) {
def map = [:]
map.destination = dest.toBase64()
map.failures = host.failures
map.successes = host.successes
map.lastAttempt = host.lastAttempt
map.lastSuccessfulAttempt = host.lastSuccessfulAttempt
def json = JsonOutput.toJson(map)
writer.println json
}