From 3ae5b8b6ea27b9d01e9bf1871d8421a698e67fca Mon Sep 17 00:00:00 2001 From: Ignas <5737899+Hoffs@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:32:37 +0200 Subject: [PATCH] fix(elbv2): pass TCP health checks on an open connection (#9032) A target group with a TCP health check was probed with an HTTP GET and judged on the response code, so a target that accepts connections but does not answer HTTP stayed unhealthy. A TCP check now opens a connection to the target and passes once it is accepted, without consulting the path or matcher, which AWS scopes to HTTP and HTTPS checks. The ELB docs describe the TCP check. Closes #9031 --- docs/services/elb.md | 11 +++-- .../services/elbv2/ElbV2HealthChecker.java | 18 ++++++- .../elbv2/ElbV2HealthCheckerTest.java | 49 +++++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/docs/services/elb.md b/docs/services/elb.md index e12fbda246..1f658b6da5 100644 --- a/docs/services/elb.md +++ b/docs/services/elb.md @@ -212,12 +212,13 @@ on the listener socket. ### Health checks -For non-Lambda targets, the checker sends an HTTP or HTTPS GET to `HealthCheckPath` -on `HealthCheckPort`, or the target's traffic port when configured as `traffic-port`. +For non-Lambda targets, the checker probes `HealthCheckPort`, or the target's traffic +port when configured as `traffic-port`. With `HealthCheckProtocol=TCP` it opens a +connection and passes once the target accepts it; `HealthCheckPath` and the matcher are +not used. Otherwise it sends an HTTP or HTTPS GET to `HealthCheckPath`, and `HealthCheckProtocol` selects TLS independently of the target's application protocol. -The response-code matcher and healthy/unhealthy thresholds affect target health. -`HealthCheckProtocol=TCP` does not perform a TCP-only probe: it sends the same GET with the -stored path and matcher, which `DescribeTargetGroups` omits for TCP checks, as AWS does. +The response-code matcher (HTTP and HTTPS checks) and healthy/unhealthy thresholds +affect target health. Lambda targets are not actively probed. In ELBv2 mock mode, sockets and active monitoring are disabled. While a target group has a `healthy` target, the proxy routes only to its `healthy` targets, so a newly registered target in `initial` diff --git a/src/main/java/io/floci/aws/services/elbv2/ElbV2HealthChecker.java b/src/main/java/io/floci/aws/services/elbv2/ElbV2HealthChecker.java index 0ddfd83d7d..665aff7801 100644 --- a/src/main/java/io/floci/aws/services/elbv2/ElbV2HealthChecker.java +++ b/src/main/java/io/floci/aws/services/elbv2/ElbV2HealthChecker.java @@ -20,7 +20,9 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; import java.net.Proxy; +import java.net.Socket; import java.net.SocketTimeoutException; import java.net.URL; import java.security.GeneralSecurityException; @@ -185,6 +187,7 @@ public class ElbV2HealthChecker implements Resettable { continue; } String protocol = tg.getHealthCheckProtocol(); + boolean tcpCheck = "TCP".equals(protocol); String path = tg.getHealthCheckPath() != null ? tg.getHealthCheckPath() : "/"; String matcher = tg.getMatcher() != null ? tg.getMatcher() : "200"; int timeout = tg.getHealthCheckTimeoutSeconds() != null ? tg.getHealthCheckTimeoutSeconds() : 5; @@ -210,7 +213,9 @@ public class ElbV2HealthChecker implements Resettable { return SocketDeadline.call(timeout, () -> { Ec2InstancePorts.Endpoint endpoint = ElbV2TargetResolver.resolve(ec2Service, instancePorts, tg, targetId, probePort); - return probeOnce(endpoint.host(), endpoint.port(), path, timeout, protocol); + return tcpCheck + ? connectOnce(endpoint.host(), endpoint.port(), timeout) + : probeOnce(endpoint.host(), endpoint.port(), path, timeout, protocol); }, () -> { synchronized (state) { state.workerRunning = false; @@ -240,7 +245,7 @@ public class ElbV2HealthChecker implements Resettable { targetId, port, outcome.cause().getMessage()); } else if (outcome.result() != null) { int statusCode = outcome.result(); - if (matchesStatusCode(statusCode, matcher)) { + if (tcpCheck || matchesStatusCode(statusCode, matcher)) { state.consecutiveFailures = 0; state.consecutiveSuccesses++; boolean initialApplicationTarget = "initial".equals(state.status) @@ -278,6 +283,15 @@ public class ElbV2HealthChecker implements Resettable { } } + /** A TCP health check passes once the connection opens; nothing is sent. */ + private static int connectOnce(String host, int port, int timeoutSeconds) throws IOException { + try (Socket socket = new Socket(Proxy.NO_PROXY)) { + socket.connect(new InetSocketAddress(ElbV2TargetResolver.resolveCheckedAddress(host), port), + timeoutSeconds * 1000); + return 0; + } + } + private int probeOnce(String host, int port, String path, int timeoutSeconds, String protocol) throws IOException { String scheme = "HTTPS".equals(protocol) ? "https" : "http"; URL url = new URL(scheme, ElbV2TargetResolver.resolveCheckedAddress(host), port, path); diff --git a/src/test/java/io/floci/aws/services/elbv2/ElbV2HealthCheckerTest.java b/src/test/java/io/floci/aws/services/elbv2/ElbV2HealthCheckerTest.java index 5c497f95fa..aff1eaa737 100644 --- a/src/test/java/io/floci/aws/services/elbv2/ElbV2HealthCheckerTest.java +++ b/src/test/java/io/floci/aws/services/elbv2/ElbV2HealthCheckerTest.java @@ -13,11 +13,15 @@ import io.vertx.core.VertxOptions; import io.vertx.core.WorkerExecutor; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerResponse; +import io.vertx.core.net.NetServer; +import io.vertx.core.net.NetSocket; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import java.net.InetAddress; +import java.net.ServerSocket; import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -113,6 +117,44 @@ class ElbV2HealthCheckerTest { } } + @Test + void tcpCheckPassesOnAnOpenConnectionWithoutSpeakingHttp() throws Exception { + NetServer server = vertx.createNetServer().connectHandler(NetSocket::close) + .listen(0, "127.0.0.1").toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + try { + ElbV2HealthChecker checker = healthChecker(); + TargetGroup targetGroup = tcpTargetGroup(server.actualPort()); + TargetDescription target = target("127.0.0.1", server.actualPort()); + + checker.addTargets(targetGroup.getTargetGroupArn(), List.of(target), targetGroup); + + await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> assertEquals("healthy", + checker.getHealth(targetGroup.getTargetGroupArn(), target.getId(), target.getPort()).state())); + } finally { + server.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS); + } + } + + @Test + void tcpCheckFailsWhenTheConnectionIsRefused() throws Exception { + int closedPort; + try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + closedPort = socket.getLocalPort(); + } + ElbV2HealthChecker checker = healthChecker(); + TargetGroup targetGroup = tcpTargetGroup(closedPort); + TargetDescription target = target("127.0.0.1", closedPort); + + checker.addTargets(targetGroup.getTargetGroupArn(), List.of(target), targetGroup); + + await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> { + ElbV2HealthChecker.TargetHealthStatus health = checker.getHealth( + targetGroup.getTargetGroupArn(), target.getId(), target.getPort()); + assertEquals("unhealthy", health.state()); + assertEquals("Target.FailedHealthChecks", health.reason()); + }); + } + @Test void responseCodeMismatchTransitionsTargetToUnhealthyWithAwsReason() throws Exception { HttpServer server = startServer(503); @@ -523,6 +565,13 @@ class ElbV2HealthCheckerTest { return targetGroup; } + private static TargetGroup tcpTargetGroup(int port) { + TargetGroup targetGroup = targetGroup(port, "200", 1, 1); + targetGroup.setProtocol("TCP"); + targetGroup.setHealthCheckProtocol("TCP"); + return targetGroup; + } + private static TargetDescription target(String id, int port) { TargetDescription target = new TargetDescription(); target.setId(id);