Skip to content
Open
Show file tree
Hide file tree
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
Expand Up @@ -2,24 +2,23 @@ package com.sksamuel.cohort.healthcheck.http

import com.sksamuel.cohort.HealthCheck
import com.sksamuel.cohort.HealthCheckResult
import io.vertx.core.Vertx
import io.vertx.core.http.HttpClient
import io.vertx.core.http.HttpClientResponse

/**
* Executes a custom http request using a Vertx HTTP client.
* The result is considered healthy if [eval] returns true, which by default looks for a 2xx status code.
* Executes a custom http request using a caller-supplied Vertx [HttpClient].
*
* The [HttpClient] is owned by the caller — its lifecycle (connection pool, netty event loops)
* is the caller's responsibility. Previously this class created its own client via
* `vertx.createHttpClient()` and never closed it, leaking pooled connections per instance.
*/
class EndpointHealthCheck(
vertx: Vertx,
private val client: HttpClient,
private val eval: suspend (HttpClientResponse) -> Boolean = { it.statusCode() in 200..299 },
override val name: String = "endpoint_request",
private val fn: suspend (HttpClient) -> HttpClientResponse,
) : HealthCheck {


private val client = vertx.createHttpClient()

override suspend fun check(): HealthCheckResult {
val resp = fn(client)
return if (eval(resp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.sksamuel.cohort.healthcheck.http

import com.sksamuel.cohort.HealthCheck
import com.sksamuel.cohort.HealthCheckResult
import io.vertx.core.Vertx
import io.vertx.core.http.HttpClient
import java.util.concurrent.atomic.AtomicBoolean

Expand All @@ -15,15 +14,17 @@ import java.util.concurrent.atomic.AtomicBoolean
*
* It does not continually ping a service, to avoid an upstream service going down, and bringing
* down all the dependent services with it in a cascading fashion.
*
* The supplied [client] is owned by the caller. Previously this class created its own client
* via `vertx.createHttpClient()` and never closed it.
*/
class EndpointStartupHealthCheck(
vertx: Vertx,
private val client: HttpClient,
override val name: String = "endpoint_startup_request",
private val fn: suspend (HttpClient) -> Boolean,
) : HealthCheck {

private val successful = AtomicBoolean(false)
private val client = vertx.createHttpClient()

override suspend fun check(): HealthCheckResult {
return if (successful.get()) {
Expand Down