Repository navigation
fix(httpclient): budget deadlines for complete retry sequences - #7266
Conversation
Aias00
left a comment
There was a problem hiding this comment.
Approved - this removes the aggregate timeout that starved the configured retries, and it does it in the only place where it can be done correctly.
Root cause, verified rather than assumed
AbstractHttpClientPlugin:91 already applies .timeout(duration, ...) to the Mono before the strategy sees it:
final Mono<R> response = doRequest(...)
.timeout(duration, Mono.error(() -> new TimeoutException("Response took longer than timeout: " + duration)))
.doOnError(e -> LOG.error(e.getMessage(), e));
...
Mono<R> retriedResponse = strategy.execute(response, exchange, duration, retryTimes);Because retryWhen resubscribes its source, that inner timeout re-arms on every attempt - so per-attempt protection was never missing. What the strategies additionally did was wrap the whole retried sequence in another timeout with the same one-attempt budget:
return response.retryWhen(retrySpec)
.timeout(duration, Mono.error(() -> new TimeoutException(...)));so with, say, a 5s attempt timeout and three attempts, the sequence was killed at 5s regardless of how many retries were configured. Replacing that outer cap with RetryTimeoutUtils.totalTimeout is exactly the missing part.
The budget actually matches the configured backoff
| strategy | max backoff configured | constant passed to totalTimeout |
|---|---|---|
DefaultRetryStrategy (current) |
Retry.backoff(...).maxBackoff(Duration.ofSeconds(20)) (:69) |
Duration.ofSeconds(20) (:78) |
ExponentialRetryBackoffStrategy |
maxBackoff(Duration.ofSeconds(5)) (:60) |
Duration.ofSeconds(5) (:51) |
FixedRetryStrategy |
Retry.fixedDelay(retryTimes, Duration.ofSeconds(2)) (:56) |
Duration.ofSeconds(2) (:52) |
Jitter (jitter(0.5d)) only ever shortens a computed delay, never exceeds maxBackoff, so (attempts * duration) + (retries * maxBackoff) really is the worst case. Overflow is saturated to Duration.ofNanos(Long.MAX_VALUE) and Duration#toNanos is inside the try (it throws its own ArithmeticException), which the new unit test pins.
The changed expectation in RetryStrategyTest is the regression test. testFixedRetryStrategyExecute uses duration = 5s with retryTimes = 3 at a 2s fixed delay: under the old code that 6s sequence hit the 5s cap and reported TimeoutException; it now reports retry exhaustion and had to change to Exceptions::isRetryExhausted. That flip is the bug being fixed, made visible - much better than asserting the new happy path only. Together with the virtual-time tests that prove all three strategies complete three retries despite backoff exceeding the attempt timeout, and that a never-completing source is still bounded, the coverage is real.
Non-blocking - two things worth writing down
- The worst-case latency envelope grew by design. With the default
HTTP_TIME_OUTof 3000 ms andretryTimes = 3, the "current" strategy budget is4 x 3s + 3 x 20s = 72sbefore the caller sees anything. PreviouslyDefaultRetryStrategyhad no aggregate cap at all (only the per-attempt one), so a hanging upstream kept the request open until the client gave up; now it terminates asGATEWAY_TIMEOUTafter 72s. Both are defensible, but this number should be visible in the docs/release note - it is the largest change an operator will actually feel. - Status mapping differs by failure mode (
DefaultRetryStrategy:81-82): blowing the sequence budget maps to504 GATEWAY_TIMEOUT, whereas exhausting retries maps toShenyuTimeoutException -> 408 REQUEST_TIMEOUT. Documenting that distinction (and the message text"Retry sequence took longer than timeout: ...") will save support engineers a lot of guessing.
CI is green on the head commit.
| }); | ||
| Duration totalTimeout = RetryTimeoutUtils.totalTimeout(duration, retryTimes, Duration.ofSeconds(20)); | ||
| return clientResponse.retryWhen(retryBackoffSpec) | ||
| .timeout(totalTimeout, Mono.error(() -> new TimeoutException("Retry sequence took longer than timeout: " + totalTimeout))) |
There was a problem hiding this comment.
Non-blocking, please write into the docs/release note: this is the first aggregate cap the default strategy has ever had (before this change it only had the per-attempt timeout applied in AbstractHttpClientPlugin.java:91), and it noticeably widens the worst-case latency.
With the defaults - HTTP_TIME_OUT = 3000 ms (AbstractHttpClientPlugin.java:80) and three retries - the budget here is 4 x 3s + 3 x 20s = 72s. Previously a hanging upstream stayed open until the client gave up; now it terminates after 72s. Bounded is better, I just want the number to be findable.
Also note the two failure modes map to different statuses below: blowing this budget becomes 504 GATEWAY_TIMEOUT, exhausting retries becomes ShenyuTimeoutException -> 408 REQUEST_TIMEOUT. The message text Retry sequence took longer than timeout: ... is what separates them in the logs - worth quoting in the docs.
Aias00
left a comment
There was a problem hiding this comment.
The retry-budget implementation and targeted tests are sound, but the current head is unmergeable because RELEASE-NOTES.md conflicts with the current master Unreleased section. Please rebase/merge current master, preserve both behavior-change entries, and rerun CI on the resolved head.
Aias00
left a comment
There was a problem hiding this comment.
Approving. Budgeting a deadline across the whole retry sequence (via RetryTimeoutUtils, with per-strategy defaults of 20s/5s/2s) is the right shape: it bounds total latency instead of re-arming a full per-attempt timeout. RetryTimeoutBudgetTest covers the arithmetic. The earlier RELEASE-NOTES conflict is resolved and CI is green.
Summary
Fixes #6583
Validation
./mvnw -pl shenyu-plugin/shenyu-plugin-httpclient -am -Dtest=RetryTimeoutBudgetTest,RetryStrategyTest,RequestBodyReplayRetryTest -DfailIfNoTests=false testThe total bound intentionally includes worst-case configured retries/backoff; large retry counts can still produce long deadlines. This does not introduce a new, separately configurable SLA or alter failover selection.