|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 dexpace and Omar Aljarrah |
| 3 | + * |
| 4 | + * Licensed under the MIT License. See LICENSE in the project root. |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | + |
| 8 | +package org.dexpace.sdk.core.http.pipeline.steps |
| 9 | + |
| 10 | +import org.dexpace.sdk.core.http.common.HttpHeaderName |
| 11 | +import java.util.Locale |
| 12 | + |
| 13 | +/** |
| 14 | + * Composable [HttpRetryConditionPredicate] that lets the server steer the retry decision via an |
| 15 | + * explicit response header (`X-Should-Retry` by default). |
| 16 | + * |
| 17 | + * Some APIs annotate responses with a definitive "retry / do not retry" signal that the client |
| 18 | + * cannot infer from the status code alone — for example a `409 Conflict` that is genuinely |
| 19 | + * transient, or a `503` the server knows will not recover. This predicate honours that signal |
| 20 | + * when, and only when, the header is present: |
| 21 | + * |
| 22 | + * - A **truthy** value (`true`, `1`, `yes`, `retry`, case-insensitive) forces a retry, even for |
| 23 | + * a status the default classifier would not retry. |
| 24 | + * - A **falsy** value (`false`, `0`, `no`, `stop`, case-insensitive) suppresses a retry, even |
| 25 | + * for a status the default classifier would retry. |
| 26 | + * - When the header is **absent**, unrecognised, or there is no response (the exception path), |
| 27 | + * the decision is delegated to [delegate] — so wiring this predicate in changes behaviour |
| 28 | + * only when the server actually speaks. |
| 29 | + * |
| 30 | + * ## What the override does and does not bypass |
| 31 | + * |
| 32 | + * The override flips only the *classification* decision — "is this response retryable?". It does |
| 33 | + * not bypass the other gates the retry step enforces: a forced retry is still capped by |
| 34 | + * [HttpRetryOptions.maxRetries], and still requires a retry-safe request (an idempotent method or |
| 35 | + * a replayable body). A truthy header on a `POST` carrying a non-replayable body therefore does |
| 36 | + * **not** trigger a retry — the body cannot be re-sent, so the response is returned as-is. |
| 37 | + * |
| 38 | + * ## Opt-in |
| 39 | + * |
| 40 | + * This predicate is **not** installed by default. A caller enables it by passing an instance as |
| 41 | + * [HttpRetryOptions.shouldRetryCondition] (and/or [HttpRetryOptions.shouldRetryException]). The |
| 42 | + * SDK's default retryable-status set is unchanged — in particular `409 Conflict` stays out of |
| 43 | + * it; this predicate is the mechanism by which a server can opt a `409` (or any other status) |
| 44 | + * into a retry, rather than widening the default set for everyone. |
| 45 | + * |
| 46 | + * ## Composition |
| 47 | + * |
| 48 | + * [delegate] defaults to the SDK's standard classifier, dispatched per path: the response |
| 49 | + * classifier (`408 / 429 / 5xx`, except `501` / `505`) on the response path, and the exception |
| 50 | + * classifier (`IOException` / `TimeoutException` anywhere in the cause chain) on the exception |
| 51 | + * path. Because the override header only appears on responses, wiring this predicate into |
| 52 | + * [HttpRetryOptions.shouldRetryException] leaves the exception-path decision entirely to the |
| 53 | + * delegate. Supply a different delegate to layer the server override on top of bespoke retry |
| 54 | + * logic, or [HttpRetryConditionPredicate] `{ false }` to make the server header the sole |
| 55 | + * authority. |
| 56 | + * |
| 57 | + * ## Thread-safety |
| 58 | + * |
| 59 | + * Immutable and stateless — safe to share across concurrent requests. |
| 60 | + * |
| 61 | + * @property headerName The response header consulted for the override signal. Defaults to |
| 62 | + * `X-Should-Retry`. |
| 63 | + * @property delegate Fallback predicate consulted when the header is absent or unrecognised, or |
| 64 | + * on the exception path. Defaults to the standard per-path classifier. |
| 65 | + */ |
| 66 | +public class ServerOverrideRetryPredicate |
| 67 | + @JvmOverloads |
| 68 | + constructor( |
| 69 | + public val headerName: HttpHeaderName = DEFAULT_HEADER_NAME, |
| 70 | + public val delegate: HttpRetryConditionPredicate = |
| 71 | + HttpRetryConditionPredicate(::defaultClassifier), |
| 72 | + ) : HttpRetryConditionPredicate { |
| 73 | + override fun shouldRetry(condition: HttpRetryCondition): Boolean { |
| 74 | + val response = condition.response ?: return delegate.shouldRetry(condition) |
| 75 | + val raw = response.headers.get(headerName) ?: return delegate.shouldRetry(condition) |
| 76 | + return when (raw.trim().lowercase(Locale.US)) { |
| 77 | + in TRUTHY -> true |
| 78 | + in FALSY -> false |
| 79 | + // An unrecognised value is not a directive — defer rather than guess. |
| 80 | + else -> delegate.shouldRetry(condition) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public companion object { |
| 85 | + /** Default override header (`X-Should-Retry`). */ |
| 86 | + @JvmField |
| 87 | + public val DEFAULT_HEADER_NAME: HttpHeaderName = HttpHeaderName.fromString("X-Should-Retry") |
| 88 | + |
| 89 | + /** Header values that force a retry. */ |
| 90 | + private val TRUTHY: Set<String> = setOf("true", "1", "yes", "retry") |
| 91 | + |
| 92 | + /** Header values that suppress a retry. */ |
| 93 | + private val FALSY: Set<String> = setOf("false", "0", "no", "stop") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +/** |
| 98 | + * Default [ServerOverrideRetryPredicate.delegate]: applies the SDK's standard classification for |
| 99 | + * whichever path the [condition] represents — the response classifier when a response is present, |
| 100 | + * the exception classifier otherwise. This keeps the override predicate's fall-through behaviour |
| 101 | + * identical to the stock [HttpRetryOptions] defaults on both the response and exception paths. |
| 102 | + */ |
| 103 | +private fun defaultClassifier(condition: HttpRetryCondition): Boolean = |
| 104 | + if (condition.response != null) { |
| 105 | + defaultShouldRetryResponse(condition) |
| 106 | + } else { |
| 107 | + defaultShouldRetryException(condition) |
| 108 | + } |
0 commit comments