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 private final int clearInterval
int failures,successes int failures,successes
long lastAttempt long lastAttempt
long lastSuccessfulAttempt
public Host(Destination destination, int clearInterval) { public Host(Destination destination, int clearInterval) {
this.destination = destination this.destination = destination
@ -20,6 +21,7 @@ class Host {
failures = 0 failures = 0
successes++ successes++
lastAttempt = System.currentTimeMillis() lastAttempt = System.currentTimeMillis()
lastSuccessfulAttempt = lastAttempt
} }
synchronized void onFailure() { synchronized void onFailure() {
@ -41,6 +43,12 @@ class Host {
} }
synchronized void canTryAgain() { synchronized void canTryAgain() {
lastSuccessfulAttempt > 0 &&
System.currentTimeMillis() - lastAttempt > (clearInterval * 60 * 1000) 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)) host.successes = Integer.valueOf(String.valueOf(entry.successes))
if (entry.lastAttempt != null) if (entry.lastAttempt != null)
host.lastAttempt = entry.lastAttempt host.lastAttempt = entry.lastAttempt
if (entry.lastSuccessfulAttempt != null)
host.lastSuccessfulAttempt = entry.lastSuccessfulAttempt
if (allowHost(host)) if (allowHost(host))
hosts.put(dest, host) hosts.put(dest, host)
} }
@ -140,12 +142,13 @@ class HostCache extends Service {
storage.delete() storage.delete()
storage.withPrintWriter { writer -> storage.withPrintWriter { writer ->
hosts.each { dest, host -> hosts.each { dest, host ->
if (allowHost(host)) { if (allowHost(host) && !host.isHopeless()) {
def map = [:] def map = [:]
map.destination = dest.toBase64() map.destination = dest.toBase64()
map.failures = host.failures map.failures = host.failures
map.successes = host.successes map.successes = host.successes
map.lastAttempt = host.lastAttempt map.lastAttempt = host.lastAttempt
map.lastSuccessfulAttempt = host.lastSuccessfulAttempt
def json = JsonOutput.toJson(map) def json = JsonOutput.toJson(map)
writer.println json writer.println json
} }