From 84cee0aa431f784b2febe1bab61cb86fb0cb8b7c Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 19 Jun 2019 08:35:31 +0100 Subject: [PATCH] retry failed hosts after one hour --- .../src/main/groovy/com/muwire/core/hostcache/Host.groovy | 8 ++++++++ .../groovy/com/muwire/core/hostcache/HostCache.groovy | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) 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 997a41a8..68e6ab89 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy @@ -5,9 +5,11 @@ import net.i2p.data.Destination class Host { private static final int MAX_FAILURES = 3 + private static final int CLEAR_INTERVAL = 60 * 60 * 1000 final Destination destination int failures,successes + long lastAttempt public Host(Destination destination) { this.destination = destination @@ -16,11 +18,13 @@ class Host { synchronized void onConnect() { failures = 0 successes++ + lastAttempt = System.currentTimeMillis() } synchronized void onFailure() { failures++ successes = 0 + lastAttempt = System.currentTimeMillis() } synchronized boolean isFailed() { @@ -34,4 +38,8 @@ class Host { synchronized void clearFailures() { failures = 0 } + + synchronized void canTryAgain() { + System.currentTimeMillis() - lastAttempt > CLEAR_INTERVAL + } } 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 1594e806..0ee76a05 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -109,6 +109,8 @@ class HostCache extends Service { Host host = new Host(dest) host.failures = Integer.valueOf(String.valueOf(entry.failures)) host.successes = Integer.valueOf(String.valueOf(entry.successes)) + if (entry.lastAttempt != null) + host.lastAttempt = entry.lastAttempt if (allowHost(host)) hosts.put(dest, host) } @@ -118,7 +120,7 @@ class HostCache extends Service { } private boolean allowHost(Host host) { - if (host.isFailed()) + if (host.isFailed() && !host.canTryAgain()) return false if (host.destination == myself) return false @@ -143,6 +145,7 @@ class HostCache extends Service { map.destination = dest.toBase64() map.failures = host.failures map.successes = host.successes + map.lastAttempt = host.lastAttempt def json = JsonOutput.toJson(map) writer.println json }