cache calls to System.currenTimeMillis()

This commit is contained in:
Zlatin Balevsky
2020-09-20 17:34:28 +01:00
parent 44af23c162
commit 3e6e0c7e9f
2 changed files with 12 additions and 9 deletions

View File

@ -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)
}
}

View File

@ -84,9 +84,10 @@ class HostCache extends Service {
List<Destination> getHosts(int n) {
List<Destination> 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<Destination> 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