Merge branch '1.5.x'
This commit is contained in:
commit
39c1757a96
|
@ -59,9 +59,7 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
|
||||||
|
|
||||||
private Map<String, HttpStatus> statusMapping = new HashMap<>();
|
private Map<String, HttpStatus> statusMapping = new HashMap<>();
|
||||||
|
|
||||||
private long lastAccess = 0;
|
private volatile CachedHealth cachedHealth;
|
||||||
|
|
||||||
private Health cached;
|
|
||||||
|
|
||||||
public HealthMvcEndpoint(HealthEndpoint delegate) {
|
public HealthMvcEndpoint(HealthEndpoint delegate) {
|
||||||
this(delegate, true);
|
this(delegate, true);
|
||||||
|
@ -164,22 +162,29 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
private Health getHealth(HttpServletRequest request, Principal principal) {
|
private Health getHealth(HttpServletRequest request, Principal principal) {
|
||||||
long accessTime = System.currentTimeMillis();
|
Health currentHealth = getCurrentHealth();
|
||||||
if (isCacheStale(accessTime)) {
|
|
||||||
this.lastAccess = accessTime;
|
|
||||||
this.cached = getDelegate().invoke();
|
|
||||||
}
|
|
||||||
if (exposeHealthDetails(request, principal)) {
|
if (exposeHealthDetails(request, principal)) {
|
||||||
return this.cached;
|
return this.cachedHealth.health;
|
||||||
}
|
}
|
||||||
return Health.status(this.cached.getStatus()).build();
|
return Health.status(currentHealth.getStatus()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isCacheStale(long accessTime) {
|
private Health getCurrentHealth() {
|
||||||
if (this.cached == null) {
|
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 cachedHealth.health;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isStale(CachedHealth cachedHealth, long accessTime) {
|
||||||
|
if (cachedHealth == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return (accessTime - this.lastAccess) >= getDelegate().getTimeToLive();
|
return (accessTime - cachedHealth.creationTime) >= getDelegate().getTimeToLive();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean exposeHealthDetails(HttpServletRequest request,
|
protected boolean exposeHealthDetails(HttpServletRequest request,
|
||||||
|
@ -214,4 +219,21 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
|
||||||
null) && principal instanceof Authentication;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue