From 1d97374857338c8dfad2590931d0432ac21522e1 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 29 Sep 2019 16:19:19 +0100 Subject: [PATCH] track last successful attempt. Only re-attempt hosts if they have ever been successful. Do not serialize hosts considered hopeless --- .../main/groovy/com/muwire/core/hostcache/Host.groovy | 10 +++++++++- .../groovy/com/muwire/core/hostcache/HostCache.groovy | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy b/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy index 524c4a05..effaef28 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy @@ -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) } } diff --git a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy index 32feeb6c..27d01dbe 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -111,7 +111,9 @@ class HostCache extends Service { host.successes = Integer.valueOf(String.valueOf(entry.successes)) if (entry.lastAttempt != null) host.lastAttempt = entry.lastAttempt - if (allowHost(host)) + 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 }