Commit 39c1757a authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.5.x'

parents b41c5d6a 7a04708c
......@@ -59,9 +59,7 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
private Map<String, HttpStatus> statusMapping = new HashMap<>();
private long lastAccess = 0;
private Health cached;
private volatile CachedHealth cachedHealth;
public HealthMvcEndpoint(HealthEndpoint delegate) {
this(delegate, true);
......@@ -164,22 +162,29 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
}
private Health getHealth(HttpServletRequest request, Principal principal) {
long accessTime = System.currentTimeMillis();
if (isCacheStale(accessTime)) {
this.lastAccess = accessTime;
this.cached = getDelegate().invoke();
}
Health currentHealth = getCurrentHealth();
if (exposeHealthDetails(request, principal)) {
return this.cached;
return this.cachedHealth.health;
}
return Health.status(currentHealth.getStatus()).build();
}
private Health getCurrentHealth() {
long accessTime = System.currentTimeMillis();
CachedHealth cachedHealth = this.cachedHealth;
if (isStale(cachedHealth, accessTime)) {
Health health = getDelegate().invoke();
this.cachedHealth = new CachedHealth(health, accessTime);
return health;
}
return Health.status(this.cached.getStatus()).build();
return cachedHealth.health;
}
private boolean isCacheStale(long accessTime) {
if (this.cached == null) {
private boolean isStale(CachedHealth cachedHealth, long accessTime) {
if (cachedHealth == null) {
return true;
}
return (accessTime - this.lastAccess) >= getDelegate().getTimeToLive();
return (accessTime - cachedHealth.creationTime) >= getDelegate().getTimeToLive();
}
protected boolean exposeHealthDetails(HttpServletRequest request,
......@@ -214,4 +219,21 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
null) && principal instanceof Authentication;
}
/**
* A cached {@link Health} that encapsulates the {@code Health} itself and the time at
* which it was created.
*/
static class CachedHealth {
private final Health health;
private final long creationTime;
CachedHealth(Health health, long creationTime) {
this.health = health;
this.creationTime = creationTime;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment