From 3e6e0c7e9f7ee6caa81d07c4b05ed906c03759c8 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 20 Sep 2020 17:34:28 +0100 Subject: [PATCH] cache calls to System.currenTimeMillis() --- .../groovy/com/muwire/core/hostcache/Host.groovy | 12 ++++++------ .../com/muwire/core/hostcache/HostCache.groovy | 9 ++++++--- 2 files changed, 12 insertions(+), 9 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 88c6defa..9b77c74d 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/Host.groovy @@ -54,17 +54,17 @@ class Host { failures = 0 } - synchronized boolean canTryAgain() { + synchronized boolean canTryAgain(final long now) { lastSuccessfulAttempt > 0 && - System.currentTimeMillis() - lastAttempt > (clearInterval * 60 * 1000) + now - lastAttempt > (clearInterval * 60 * 1000) } - synchronized boolean isHopeless() { + synchronized boolean isHopeless(final long now) { isFailed() && - System.currentTimeMillis() - lastSuccessfulAttempt > (hopelessInterval * 60 * 1000) + now - lastSuccessfulAttempt > (hopelessInterval * 60 * 1000) } - synchronized boolean isRecentlyRejected() { - System.currentTimeMillis() - lastRejection < (rejectionInterval * 60 * 1000) + synchronized boolean isRecentlyRejected(final long now) { + now - lastRejection < (rejectionInterval * 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 ce342226..265af706 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -84,9 +84,10 @@ class HostCache extends Service { List getHosts(int n) { List rv = new ArrayList<>(hosts.keySet()) rv.retainAll {allowHost(hosts[it])} + final long now = System.currentTimeMillis() rv.removeAll { def h = hosts[it]; - (h.isFailed() && !h.canTryAgain()) || h.isRecentlyRejected() || h.isHopeless() + (h.isFailed() && !h.canTryAgain(now)) || h.isRecentlyRejected(now) || h.isHopeless(now) } if (rv.size() <= n) return rv @@ -116,8 +117,9 @@ class HostCache extends Service { int countHopelessHosts() { List rv = new ArrayList<>(hosts.keySet()) + final long now = System.currentTimeMillis() rv.retainAll { - hosts[it].isHopeless() + hosts[it].isHopeless(now) } rv.size() } @@ -162,9 +164,10 @@ class HostCache extends Service { private void save() { storage.delete() + final long now = System.currentTimeMillis() storage.withPrintWriter { writer -> hosts.each { dest, host -> - if (allowHost(host) && !host.isHopeless()) { + if (allowHost(host) && !host.isHopeless(now)) { def map = [:] map.destination = dest.toBase64() map.failures = host.failures