-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from sladkoff/feature/health-checks
feat: health checks
- Loading branch information
Showing
8 changed files
with
184 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.sldk.mc; | ||
|
||
import de.sldk.mc.health.HealthChecks; | ||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.eclipse.jetty.server.Handler; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.Response; | ||
import org.eclipse.jetty.util.Callback; | ||
|
||
public class HealthController extends Handler.Abstract { | ||
|
||
private final HealthChecks checks; | ||
|
||
private HealthController(final HealthChecks checks) { | ||
this.checks = checks; | ||
} | ||
|
||
public static Handler create(final HealthChecks checks) { | ||
return new HealthController(checks); | ||
} | ||
|
||
@Override | ||
public boolean handle(Request request, Response response, Callback callback) throws Exception { | ||
response.setStatus(checks.isHealthy() ? HttpStatus.OK_200 : HttpStatus.SERVICE_UNAVAILABLE_503); | ||
callback.succeeded(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/de/sldk/mc/health/ConcurrentHealthChecks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.sldk.mc.health; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public final class ConcurrentHealthChecks implements HealthChecks { | ||
|
||
private final Set<HealthCheck> checks; | ||
|
||
private ConcurrentHealthChecks(final Set<HealthCheck> checks) { | ||
this.checks = checks; | ||
} | ||
|
||
public static HealthChecks create() { | ||
return new ConcurrentHealthChecks(ConcurrentHashMap.newKeySet()); | ||
} | ||
|
||
@Override | ||
public boolean isHealthy() { | ||
for (final HealthCheck check : checks) if (!check.isHealthy()) return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void add(final HealthCheck check) { | ||
checks.add(check); | ||
} | ||
|
||
@Override | ||
public void remove(final HealthCheck check) { | ||
checks.remove(check); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package de.sldk.mc.health; | ||
|
||
/** | ||
* Health check. | ||
*/ | ||
public interface HealthCheck { | ||
|
||
/** | ||
* Checks if the current state is healthy. | ||
* | ||
* @return {@code true} if the state is healthy and {@code false} otherwise | ||
*/ | ||
boolean isHealthy(); | ||
|
||
/** | ||
* Creates a health compound check from the provided ones reporting healthy status if all the checks report it. | ||
* | ||
* @param checks merged health checks | ||
* @return compound health check | ||
*/ | ||
static HealthCheck allOf(final HealthCheck... checks) { | ||
return new AllOf(checks); | ||
} | ||
|
||
/** | ||
* Creates a compound health check from the provided ones reporting healthy status if any check reports it. | ||
* | ||
* @param checks merged health checks | ||
* @return compound health check | ||
*/ | ||
static HealthCheck anyOf(final HealthCheck... checks) { | ||
return new AnyOf(checks); | ||
} | ||
|
||
final class AllOf implements HealthCheck { | ||
private final HealthCheck[] checks; | ||
|
||
private AllOf(final HealthCheck[] checks) { | ||
this.checks = checks; | ||
} | ||
|
||
@Override | ||
public boolean isHealthy() { | ||
for (final HealthCheck check : checks) if (!check.isHealthy()) return false; | ||
|
||
return true; | ||
} | ||
} | ||
|
||
final class AnyOf implements HealthCheck { | ||
private final HealthCheck[] checks; | ||
|
||
private AnyOf(final HealthCheck[] checks) { | ||
this.checks = checks; | ||
} | ||
|
||
@Override | ||
public boolean isHealthy() { | ||
for (final HealthCheck check : checks) if (check.isHealthy()) return true; | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package de.sldk.mc.health; | ||
|
||
/** | ||
* Dynamic compound health checks. | ||
*/ | ||
public interface HealthChecks extends HealthCheck { | ||
|
||
/** | ||
* Adds the provided health check to this one. | ||
* | ||
* @param check added health check | ||
*/ | ||
void add(HealthCheck check); | ||
|
||
/** | ||
* Removes the provided health check from this one. | ||
* | ||
* @param check removed health check | ||
*/ | ||
void remove(HealthCheck check); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters