Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async health check #973

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.dropwizard.metrics.health;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class AsyncHealthCheck extends HealthCheck implements Runnable {
private final HealthCheck synchronousHealthCheck;
private final int maxStaleAge;
private final TimeUnit maxStaleAgeUnit;

private volatile CachedResult lastResult;

public AsyncHealthCheck(HealthCheck synchronousHealthCheck, int maxStaleAge, TimeUnit maxStaleAgeUnit) {
this.synchronousHealthCheck = synchronousHealthCheck;
this.maxStaleAge = maxStaleAge;
this.maxStaleAgeUnit = maxStaleAgeUnit;
}

@Override
protected Result check() throws Exception {
if (new Date(lastResult.whenCheckFinished.getTime() + maxStaleAgeUnit.toSeconds(maxStaleAge)).before(new Date())) {
return Result.unhealthy("Stale");
} else {
return lastResult.result;
}
}

@Override
public void run() {
Result check = synchronousHealthCheck.execute();
lastResult = new CachedResult(check, new Date());
}

private static class CachedResult {
public final Result result;
public final Date whenCheckFinished;

private CachedResult(Result result, Date whenCheckFinished) {
this.result = result;
this.whenCheckFinished = whenCheckFinished;
}
}
}