diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java index 6b28ad8f..943f3a5d 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java @@ -22,6 +22,11 @@ import org.springframework.http.client.JdkClientHttpRequestFactory; * timeout, remaining deadline)} from {@link AttemptDeadline}. The factory only holds references; * the client and the write executor are shared. * + *

That timer is the only thing that ends a JDK attempt at its deadline, and it keeps whole + * milliseconds. {@link AttemptDeadline#bound(Duration)} rounds the remaining time up for that + * reason: truncated, the timer fired just before the deadline and the attempt was reported as a + * response timeout rather than the deadline. + * *

A streaming download cannot use that request. Its body is handed to the caller and may run for * minutes, but Spring's timer only stops when the body stream is closed, so every JDK download * longer than the response-header timeout was cut mid-body (review 2026-11-03 RR-04-03). Inside a diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java index 36d072bb..b2ac3dfa 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java @@ -1,6 +1,7 @@ package dev.caskeleton.adapter.outbound.httpclient.transport; import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -83,14 +84,30 @@ public final class AttemptDeadline { return Optional.ofNullable(CURRENT.get()).map(Scope::remaining); } - /** A stage timeout no longer than the current attempt's remaining time. */ + /** + * A stage timeout cut down to the current attempt's remaining time when that is shorter. + * + *

A cut-down value is rounded up to the next whole millisecond, so an engine timer armed from + * it never ends before the deadline. Engines keep their timeouts in milliseconds — Spring's JDK + * request arms its timer with {@code Duration.toMillis()} — and the truncated remainder made that + * timer fire just before the deadline. The executor names the deadline by asking the scope after + * the engine gave up, so such an attempt was reported as a response timeout on a healthy upstream + * and the circuit breaker counted the caller's own deadline against it. Ending under a + * millisecond late costs nothing: by then the scope reports itself expired. + */ public static Duration bound(Duration stageTimeout) { Objects.requireNonNull(stageTimeout, "stage timeout"); return remaining() .filter(remaining -> remaining.compareTo(stageTimeout) < 0) + .map(AttemptDeadline::toWholeMillisecondsUp) .orElse(stageTimeout); } + private static Duration toWholeMillisecondsUp(Duration duration) { + Duration truncated = duration.truncatedTo(ChronoUnit.MILLIS); + return truncated.equals(duration) ? duration : truncated.plusMillis(1); + } + /** * Registers how to abort the exchange the current attempt is about to start. * diff --git a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java index 388bb2c6..fa2cfc77 100644 --- a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java +++ b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java @@ -6,6 +6,7 @@ import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.LockSupport; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -41,6 +42,31 @@ class AttemptDeadlineTest { } } + @Test + @DisplayName("bound 값을 밀리초로 잘라 건 엔진 timer도 attempt deadline보다 먼저 끝나지 않는다") + void anEngineTimerArmedInWholeMillisecondsDoesNotEndBeforeTheDeadline() { + // Spring's JDK request arms its timer with Duration.toMillis(). A remaining time with a + // sub-millisecond part lost it there, the engine gave up just before the deadline, and the + // executor, asking the scope a moment later, reported a response timeout instead. + try (AttemptDeadline.Scope scope = + AttemptDeadline.open(Duration.ofMillis(20).plusNanos(900_000))) { + // given + Duration bounded = AttemptDeadline.bound(Duration.ofSeconds(2)); + long armedAt = System.nanoTime(); + long firesAt = armedAt + TimeUnit.MILLISECONDS.toNanos(bounded.toMillis()); + + // when + while (System.nanoTime() - firesAt < 0) { + LockSupport.parkNanos(firesAt - System.nanoTime()); + } + + // then + assertThat(scope.expired()) + .as("deadline passed when a whole-millisecond timer from bound() = %s fired", bounded) + .isTrue(); + } + } + @Test @DisplayName("deadline이 지나면 등록된 중단 동작을 실행하고 scope는 만료로 보고한다") void theRegisteredAbortRunsWhenTheDeadlinePasses() throws Exception {