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
This commit is contained in:
Ignas authored and GitHub committed 2026-11-20 07:32:37 -04:00
1 parent 4713120f3e
commit 3ae5b8b6ea
3 files changed
+71 -7

No files matched your search

@@ -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);