diff --git a/docs/upgrade/2026-11-httpclient-failure-classification.md b/docs/upgrade/2026-11-httpclient-failure-classification.md new file mode 100644 index 00000000..6ba4065a --- /dev/null +++ b/docs/upgrade/2026-11-httpclient-failure-classification.md @@ -0,0 +1,26 @@ +# 업그레이드 안내 — 2026-11 HTTP client 전송 실패 분류 + +- 범위: Stable transport 두 개(Apache·JDK)의 전송 실패 분류를 공용 규칙 하나로 합친 변경. 설정 키와 공개 타입은 + 그대로다. 같은 실패에 두 transport가 같은 예외와 재시도 판정을 낸다. 따라 고칠 코드는 없다. +- 근거: 2026-11-21 리뷰 발견 REV-BE-0024(본문 중 끊김의 분류가 transport마다 달랐다). 본문 중 끊김이 + `HttpResponseTruncatedException`이라는 것은 `docs/httpclient/operations.md` 「예외」 표와 + `docs/httpclient/configuration-reference.md`가 이미 약속한 응답이다. + +## 1. 누가 읽나 + +이 템플릿을 `upstream` remote로 두고 merge해 받는 파생 서비스가 읽는다. 특히 HTTP client를 Apache transport로 +쓰는 서비스, 호출 thread를 interrupt해서 호출을 취소하는 서비스다. + +## 2. 바뀐 동작 (따라 할 일 없음) + +| # | 바뀐 것 | 전에는 | 해당 서비스 | +| --- | --- | --- | --- | +| 1 | Apache transport에서 응답 본문을 받다가 엔진이 알아보지 못한 이유(`connection reset` 등)로 끊기면 `HttpResponseTruncatedException`이다. 문서가 약속한 타입이고 JDK transport와 같다. 버퍼 본문 호출은 안전하게 멱등이면 재시도 예산 안에서 다시 보낸다. 넘긴 뒤의 스트리밍 본문은 `HttpBodyReadException`의 `failure()`가 이 타입이다 | `HttpUnknownTransportException`이었고 재시도하지 않았다 | Apache transport를 쓰는 서비스 | +| 2 | JDK transport에서 본문을 다 읽은 뒤(단계 `COMPLETE`) 알아보지 못한 실패는 `HttpUnknownTransportException`이다. Apache와 같다 | `HttpResponseTruncatedException`이었다 | JDK transport를 쓰는 서비스 | +| 3 | 호출 thread의 interrupt 플래그가 선 채 알아보지 못한 실패로 끝난 시도는 취소(category `CANCELLED`, 사유 `ATTEMPT_INTERRUPTED`)다. 호출자는 `HttpUnknownTransportException`을 받고, 재시도하지 않는다. virtual thread가 socket을 읽다 interrupt되면 JDK가 socket을 닫고 `SocketException`을 던지는 경우가 이것이다 | JDK transport에서는 본문 중이면 `HttpResponseTruncatedException`이라 버퍼 본문의 멱등 호출을 다시 보냈다. Apache transport는 `HttpUnknownTransportException`이었다 | 호출 thread를 interrupt해 호출을 취소하는 서비스 | + +## 3. 바뀌지 않은 것 + +- 예외 타입 목록, 설정 키, `DefaultRetryEligibilityEngine`의 재시도 판정 규칙은 그대로다. 바뀐 것은 어떤 실패가 어느 + category가 되느냐다. +- 실험 transport인 Jetty HTTP/3의 분류는 이번에 바꾸지 않았다. diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/apache/ApacheFailureClassifier.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/apache/ApacheFailureClassifier.java index d08a53fa..f35d0490 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/apache/ApacheFailureClassifier.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/apache/ApacheFailureClassifier.java @@ -1,19 +1,10 @@ package dev.caskeleton.adapter.outbound.httpclient.apache; import dev.caskeleton.adapter.outbound.httpclient.api.operation.AttemptStage; -import dev.caskeleton.adapter.outbound.httpclient.api.operation.ExecutionEvidence; import dev.caskeleton.adapter.outbound.httpclient.api.operation.FailureCategory; import dev.caskeleton.adapter.outbound.httpclient.transport.TransportFailure; import dev.caskeleton.adapter.outbound.httpclient.transport.TransportFailureClassifier; -import java.io.InterruptedIOException; -import java.net.ConnectException; -import java.net.NoRouteToHostException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; -import java.security.cert.CertificateException; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLHandshakeException; -import javax.net.ssl.SSLPeerUnverifiedException; +import dev.caskeleton.adapter.outbound.httpclient.transport.TransportFailureRules; import org.apache.hc.client5.http.ConnectTimeoutException; import org.apache.hc.client5.http.HttpHostConnectException; import org.apache.hc.core5.http.ConnectionClosedException; @@ -21,28 +12,18 @@ import org.apache.hc.core5.http.ConnectionClosedException; /** * Maps Apache HttpClient 5 failures onto stable evidence (design §13.3, §13.4). * - *
The classifier is deliberately asymmetric: {@code NOT_SENT} is only produced for stages that - * prove nothing left the process. Once the request write has begun, an ambiguous I/O error stays - * {@code SENT_NO_RESPONSE}, because guessing "not sent" is what turns a timeout into a duplicate - * payment. + *
Only Apache's own exception types are recognised here. The cause chain, the JDK socket and TLS
+ * exceptions, and the stage rules are shared with the JDK engine in {@link TransportFailureRules}.
*/
public final class ApacheFailureClassifier implements TransportFailureClassifier {
@Override
public TransportFailure classify(Throwable failure, AttemptStage lastObservedStage) {
- // The whole cause chain is inspected, not just the outermost throwable: Spring wraps engine
- // exceptions, and a wrapped ConnectException still proves the request was never sent. Matching
- // only the outer type would downgrade a provable NOT_SENT to an ambiguous SENT_NO_RESPONSE.
- for (Throwable cause : chain(failure)) {
- TransportFailure recognized = recognize(cause, lastObservedStage);
- if (recognized != null) {
- return recognized;
- }
- }
- return conservative(lastObservedStage, FailureCategory.UNKNOWN, "TRANSPORT_FAILURE");
+ return TransportFailureRules.classify(
+ failure, lastObservedStage, ApacheFailureClassifier::recognize);
}
- private TransportFailure recognize(Throwable cause, AttemptStage lastObservedStage) {
+ private static TransportFailure recognize(Throwable cause, AttemptStage lastObservedStage) {
if (cause instanceof org.apache.hc.core5.concurrent.CancellableDependency) {
return TransportFailure.notSent(
AttemptStage.POOL_ACQUIRE, FailureCategory.POOL_ACQUIRE_TIMEOUT, "POOL_ACQUIRE_TIMEOUT");
@@ -51,90 +32,21 @@ public final class ApacheFailureClassifier implements TransportFailureClassifier
return TransportFailure.notSent(
AttemptStage.POOL_ACQUIRE, FailureCategory.POOL_ACQUIRE_TIMEOUT, "POOL_ACQUIRE_TIMEOUT");
}
- if (cause instanceof UnknownHostException) {
- return TransportFailure.notSent(
- AttemptStage.DNS, FailureCategory.DNS, "DNS_RESOLUTION_FAILED");
- }
- if (cause instanceof ConnectTimeoutException
- || cause instanceof HttpHostConnectException
- || cause instanceof ConnectException
- || cause instanceof NoRouteToHostException) {
+ // Both connect failures. ConnectTimeoutException is a SocketTimeoutException, so it is
+ // recognised here, before the shared timeout rule would classify it by the stage.
+ if (cause instanceof ConnectTimeoutException || cause instanceof HttpHostConnectException) {
return TransportFailure.notSent(
AttemptStage.CONNECT, FailureCategory.CONNECT, "CONNECT_FAILED");
}
- if (cause instanceof SSLPeerUnverifiedException || cause instanceof CertificateException) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_TRUST_FAILED");
- }
- if (cause instanceof SSLHandshakeException) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_HANDSHAKE_FAILED");
- }
- if (cause instanceof SSLException
- && !lastObservedStage.isAtLeast(AttemptStage.REQUEST_HEADERS)) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_TRANSIENT, "TLS_TRANSIENT_FAILURE");
- }
- if (cause instanceof SocketTimeoutException) {
- return timeout(lastObservedStage);
- }
- if (cause instanceof InterruptedIOException) {
- // Not a timeout. A SocketTimeoutException means the peer went quiet; a bare
- // InterruptedIOException usually means this thread was interrupted — a cancellation, from a
- // caller or a shutdown. Treating the two alike classified a cancellation as a transient
- // timeout, which the retry engine then retried, so cancelling a call could produce more
- // requests than not cancelling it. The interrupt flag is restored because swallowing it
- // leaves the thread unable to observe its own cancellation.
- Thread.currentThread().interrupt();
- return conservative(lastObservedStage, FailureCategory.CANCELLED, "ATTEMPT_INTERRUPTED");
- }
if (cause instanceof ConnectionClosedException) {
- return conservative(
+ return TransportFailureRules.conservative(
lastObservedStage, FailureCategory.RESPONSE_TRUNCATED, "CONNECTION_CLOSED");
}
return null;
}
- private java.util.List The JDK exposes fewer stage-specific exceptions than Apache, so anything other than a proven
- * pre-send failure stays conservative.
+ * pre-send failure stays conservative. Only the client's own timeout types are recognised here; the
+ * cause chain, the socket and TLS exceptions, and the stage rules are shared with the Apache engine
+ * in {@link TransportFailureRules}.
*/
public final class JdkFailureClassifier implements TransportFailureClassifier {
@Override
public TransportFailure classify(Throwable failure, AttemptStage lastObservedStage) {
- // Spring and the JDK both wrap engine exceptions; the chain is inspected so a wrapped
- // ConnectException still classifies as provably NOT_SENT.
- for (Throwable cause : chain(failure)) {
- TransportFailure recognized = recognize(cause, lastObservedStage);
- if (recognized != null) {
- return recognized;
- }
- }
- return fallback(lastObservedStage);
+ return TransportFailureRules.classify(
+ failure, lastObservedStage, JdkFailureClassifier::recognize);
}
- private TransportFailure recognize(Throwable cause, AttemptStage lastObservedStage) {
- if (cause instanceof HttpConnectTimeoutException
- || cause instanceof ConnectException
- || cause instanceof NoRouteToHostException) {
+ private static TransportFailure recognize(Throwable cause, AttemptStage lastObservedStage) {
+ // HttpConnectTimeoutException extends HttpTimeoutException, so the connect case comes first.
+ if (cause instanceof HttpConnectTimeoutException) {
return TransportFailure.notSent(
AttemptStage.CONNECT, FailureCategory.CONNECT, "CONNECT_FAILED");
}
- if (cause instanceof UnknownHostException) {
- return TransportFailure.notSent(
- AttemptStage.DNS, FailureCategory.DNS, "DNS_RESOLUTION_FAILED");
- }
- if (cause instanceof SSLPeerUnverifiedException || cause instanceof CertificateException) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_TRUST_FAILED");
- }
- if (cause instanceof SSLHandshakeException) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_HANDSHAKE_FAILED");
- }
- if (cause instanceof SSLException
- && !lastObservedStage.isAtLeast(AttemptStage.REQUEST_HEADERS)) {
- return TransportFailure.notSent(
- AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_TRANSIENT, "TLS_TRANSIENT_FAILURE");
- }
- // HttpTimeoutException is an IOException, never an InterruptedIOException, so only the socket
- // timeout needs excluding here; the JDK client's own timeouts are classified below.
- if (cause instanceof InterruptedIOException && !(cause instanceof SocketTimeoutException)) {
- // Cancellation, not a timeout. Classifying it as a timeout let the retry engine reissue a
- // request the caller had just cancelled, and dropped the interrupt so the thread could not
- // see its own cancellation either.
- Thread.currentThread().interrupt();
- if (lastObservedStage.provesNotSent()) {
- return TransportFailure.notSent(
- lastObservedStage, FailureCategory.CANCELLED, "ATTEMPT_INTERRUPTED");
- }
- return TransportFailure.sentNoResponse(
- lastObservedStage, FailureCategory.CANCELLED, "ATTEMPT_INTERRUPTED");
- }
- if (cause instanceof HttpTimeoutException || cause instanceof SocketTimeoutException) {
- if (lastObservedStage.isAtLeast(AttemptStage.RESPONSE_BODY)) {
- return new TransportFailure(
- lastObservedStage,
- ExecutionEvidence.PARTIAL_RESPONSE,
- FailureCategory.RESPONSE_TIMEOUT,
- "RESPONSE_BODY_TIMEOUT");
- }
- return TransportFailure.sentNoResponse(
- AttemptStage.RESPONSE_HEADERS,
- FailureCategory.RESPONSE_TIMEOUT,
- "RESPONSE_HEADER_TIMEOUT");
+ // An IOException, never an InterruptedIOException, so the shared cancellation rule would not
+ // see it; it is the client's request timeout and reads like a socket timeout.
+ if (cause instanceof HttpTimeoutException) {
+ return TransportFailureRules.timeout(lastObservedStage);
}
return null;
}
-
- private TransportFailure fallback(AttemptStage lastObservedStage) {
- if (lastObservedStage.provesNotSent()) {
- return TransportFailure.notSent(
- lastObservedStage, FailureCategory.UNKNOWN, "TRANSPORT_FAILURE");
- }
- if (lastObservedStage.isAtLeast(AttemptStage.RESPONSE_BODY)) {
- return new TransportFailure(
- lastObservedStage,
- ExecutionEvidence.PARTIAL_RESPONSE,
- FailureCategory.RESPONSE_TRUNCATED,
- "TRANSPORT_FAILURE");
- }
- return TransportFailure.sentNoResponse(
- lastObservedStage, FailureCategory.UNKNOWN, "TRANSPORT_FAILURE");
- }
-
- private java.util.List An engine classifier contributes only what is particular to its engine: the exception types it
+ * alone throws. The rest lives here once — walking the cause chain, the JDK socket and TLS
+ * exceptions both engines surface, and the rules that turn a stage into evidence. Each engine used
+ * to carry its own copy of all of it, and the copies drifted: a failure nobody recognised while the
+ * response body was arriving was {@code UNKNOWN} on Apache and {@code RESPONSE_TRUNCATED} on JDK,
+ * so the same connection reset was a permanent failure on one engine and a retryable one on the
+ * other. {@code TransportFailureClassifierContractTest} holds both engines to one table. The
+ * experimental HTTP/3 classifier does not use these rules yet.
+ */
+public final class TransportFailureRules {
+
+ private TransportFailureRules() {}
+
+ /**
+ * One engine's recognition of its own exception types.
+ *
+ * Answers {@code null} for a cause that is not one of them, so the shared rules try it next.
+ */
+ @FunctionalInterface
+ public interface EngineRecognizer {
+
+ TransportFailure recognize(Throwable cause, AttemptStage lastObservedStage);
+ }
+
+ /**
+ * Classifies {@code failure} by the first cause in its chain that the engine or the shared rules
+ * recognise, outermost first. When none is recognised, a thread whose interrupt flag is set makes
+ * it a cancellation; otherwise the stage alone decides.
+ *
+ * The whole chain is read because Spring and the engines wrap their exceptions: a wrapped
+ * {@code ConnectException} still proves the request never left, and matching only the outer type
+ * would downgrade that to an ambiguous {@code SENT_NO_RESPONSE}. For each cause the engine's
+ * types are tried before the shared ones, so an engine exception that extends a JDK one is read
+ * as the engine meant it: Apache's pool timeout is an {@code InterruptedIOException} the shared
+ * rules would read as a cancellation, and its connect timeout a {@code SocketTimeoutException}
+ * they would read by the stage.
+ */
+ public static TransportFailure classify(
+ Throwable failure, AttemptStage lastObservedStage, EngineRecognizer engine) {
+ for (Throwable cause : causeChain(failure)) {
+ TransportFailure recognized = engine.recognize(cause, lastObservedStage);
+ if (recognized == null) {
+ recognized = recognizeShared(cause, lastObservedStage);
+ }
+ if (recognized != null) {
+ return recognized;
+ }
+ }
+ if (Thread.currentThread().isInterrupted()) {
+ // Failures are classified on the thread that made the attempt, so a set flag means
+ // the attempt was cancelled, and a cancellation must not be retried. Interrupting a
+ // virtual thread blocked on a socket closes the socket: the read fails with
+ // SocketException("Closed by interrupt"), or ClosedByInterruptException on a channel,
+ // and the flag stays set. Neither is an InterruptedIOException, so read by the stage
+ // alone it was a truncated body, which the retry engine retries: cancelling a call
+ // sent it again. The flag is left as it is.
+ return conservative(lastObservedStage, FailureCategory.CANCELLED, "ATTEMPT_INTERRUPTED");
+ }
+ return unrecognized(lastObservedStage);
+ }
+
+ /**
+ * A timeout, read against the stage it happened in.
+ *
+ * Before anything left it was the connect; while the body was arriving the response was partly
+ * received; anywhere between, the request may have been processed and no headers came back.
+ */
+ public static TransportFailure timeout(AttemptStage lastObservedStage) {
+ if (lastObservedStage.provesNotSent()) {
+ return TransportFailure.notSent(
+ lastObservedStage, FailureCategory.CONNECT, "CONNECT_TIMEOUT");
+ }
+ if (lastObservedStage.isAtLeast(AttemptStage.RESPONSE_BODY)) {
+ return new TransportFailure(
+ lastObservedStage,
+ ExecutionEvidence.PARTIAL_RESPONSE,
+ FailureCategory.RESPONSE_TIMEOUT,
+ "RESPONSE_BODY_TIMEOUT");
+ }
+ return TransportFailure.sentNoResponse(
+ AttemptStage.RESPONSE_HEADERS, FailureCategory.RESPONSE_TIMEOUT, "RESPONSE_HEADER_TIMEOUT");
+ }
+
+ /**
+ * A failure whose evidence only the stage can give.
+ *
+ * Deliberately asymmetric: {@code NOT_SENT} is only produced for a stage that proves nothing
+ * left the process. Once the request write has begun the failure stays {@code SENT_NO_RESPONSE},
+ * because guessing "not sent" is what turns a timeout into a duplicate payment; once the body was
+ * arriving, the response was partly received.
+ */
+ public static TransportFailure conservative(
+ AttemptStage lastObservedStage, FailureCategory category, String reason) {
+ if (lastObservedStage.provesNotSent()) {
+ return TransportFailure.notSent(lastObservedStage, category, reason);
+ }
+ if (lastObservedStage.isAtLeast(AttemptStage.RESPONSE_BODY)) {
+ return new TransportFailure(
+ lastObservedStage, ExecutionEvidence.PARTIAL_RESPONSE, category, reason);
+ }
+ return TransportFailure.sentNoResponse(lastObservedStage, category, reason);
+ }
+
+ private static TransportFailure recognizeShared(Throwable cause, AttemptStage lastObservedStage) {
+ if (cause instanceof UnknownHostException) {
+ return TransportFailure.notSent(
+ AttemptStage.DNS, FailureCategory.DNS, "DNS_RESOLUTION_FAILED");
+ }
+ if (cause instanceof ConnectException || cause instanceof NoRouteToHostException) {
+ return TransportFailure.notSent(
+ AttemptStage.CONNECT, FailureCategory.CONNECT, "CONNECT_FAILED");
+ }
+ if (cause instanceof SSLPeerUnverifiedException || cause instanceof CertificateException) {
+ return TransportFailure.notSent(
+ AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_TRUST_FAILED");
+ }
+ if (cause instanceof SSLHandshakeException) {
+ return TransportFailure.notSent(
+ AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_PERMANENT, "TLS_HANDSHAKE_FAILED");
+ }
+ if (cause instanceof SSLException
+ && !lastObservedStage.isAtLeast(AttemptStage.REQUEST_HEADERS)) {
+ return TransportFailure.notSent(
+ AttemptStage.TLS_HANDSHAKE, FailureCategory.TLS_TRANSIENT, "TLS_TRANSIENT_FAILURE");
+ }
+ if (cause instanceof SocketTimeoutException) {
+ return timeout(lastObservedStage);
+ }
+ if (cause instanceof InterruptedIOException) {
+ // Not a timeout. A SocketTimeoutException means the peer went quiet; a bare
+ // InterruptedIOException usually means this thread was interrupted — a cancellation, from a
+ // caller or a shutdown. Treating the two alike classified a cancellation as a transient
+ // timeout, which the retry engine then retried, so cancelling a call could produce more
+ // requests than not cancelling it. The interrupt flag is restored because swallowing it
+ // leaves the thread unable to observe its own cancellation.
+ Thread.currentThread().interrupt();
+ return conservative(lastObservedStage, FailureCategory.CANCELLED, "ATTEMPT_INTERRUPTED");
+ }
+ return null;
+ }
+
+ private static TransportFailure unrecognized(AttemptStage lastObservedStage) {
+ // While the body was arriving, a failure nobody recognised is a response that stopped short —
+ // RESPONSE_TRUNCATED in the design's category table, and what Apache already reported for its
+ // own ConnectionClosedException. At any other stage, COMPLETE included, it proves nothing
+ // beyond
+ // what the stage proves: a read body did not stop short.
+ FailureCategory category =
+ lastObservedStage == AttemptStage.RESPONSE_BODY
+ ? FailureCategory.RESPONSE_TRUNCATED
+ : FailureCategory.UNKNOWN;
+ return conservative(lastObservedStage, category, "TRANSPORT_FAILURE");
+ }
+
+ private static List The table covers what the engines share: the cause chain, the JDK socket and TLS exceptions
+ * every engine surfaces, and the rules that turn a stage into evidence. An engine's own exception
+ * types are not in it. The engines used to keep a copy of these rules each, and the copies drifted
+ * with no test seeing it: a connection reset while the body was arriving was {@code UNKNOWN}, a
+ * permanent failure, on Apache and a retryable {@code RESPONSE_TRUNCATED} on JDK. A unit test per
+ * engine would only have pinned the two answers apart; one table for both pins them together.
+ *
+ * Selection follows the transport contract lane ({@link HttpClientContract}). The scripted fake
+ * is not held to this table: its classifier answers a script rather than a stage, and the fake is
+ * held to the transport contract instead ({@code ScriptedTransportContractTest}).
+ */
+@Tag("httpclient-contract")
+@ParameterizedClass
+@MethodSource("transports")
+class TransportFailureClassifierContractTest {
+
+ private static final String RESET = "Connection reset";
+
+ @Parameter String transport;
+
+ static List