-
public class StreamRetryPolicy.Companion
-
-
Field Summary
Fields Modifier and Type Field Description public final static StreamRetryPolicy.CompanionINSTANCE
-
Method Summary
Modifier and Type Method Description final StreamRetryPolicyexponential(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)Creates an exponential back-off policy. final StreamRetryPolicyquadratic(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)Creates a quadratic back-off policy. final StreamRetryPolicylinear(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)Creates a linear back-off policy. final StreamRetryPolicyfixed(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long delayMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)Fixed-delay back-off: every retry waits exactly delayMillis, clamped to maxBackoffMillis for safety. final StreamRetryPolicycustom(Integer minRetries, Integer maxRetries, Long minBackoffMills, Long maxBackoffMills, Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp, Function2<Integer, Long, Long> nextDelay)Custom builder – explicitly specify every parameter while still benefiting from the invariant checks in the private constructor. -
-
Method Detail
-
exponential
final StreamRetryPolicy exponential(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)
Creates an exponential back-off policy.
Delay doubles on every retry:
nextDelay = backoffStepMillis × 2^(retryIndex - 1)then clamped to
[backoffStepMillis, maxBackoffMillis].Example with defaults:
attempt 1 → 0 ms retry 1 → 250 ms retry 2 → 500 ms retry 3 → 1 000 ms retry 4 → 2 000 ms …- Parameters:
minRetries- Minimum retry attempts (inclusive).maxRetries- Maximum retry attempts (inclusive).backoffStepMillis- Base step that controls growth steepness.maxBackoffMillis- Upper bound for any delay.initialDelayMillis- Delay before the first retry.giveUp- Custom predicate to override the default “> maxRetries”.
-
quadratic
final StreamRetryPolicy quadratic(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)
Creates a quadratic back-off policy.
Each retry adds a growing increment to the previous delay:
nextDelay = prevDelay + retryIndex × backoffStepMillisthen clamped to
[backoffStepMillis, maxBackoffMillis]. Driven from an initialDelayMillis of0, the delays are the triangular numbers scaled by the step —backoffStepMillis × n(n + 1) / 2.Asymptotically this is the gentler curve, but over the range a retry loop actually covers it is the steeper of the two. At a 100 ms step it runs
100, 300, 600, 1000against exponential's100, 200, 400, 800; doubling only overtakes it at retry 5, and with the defaultmaxRetries = 5the processor never computes that retry. Expect longer waits than exponential, not shorter.Example with defaults:
attempt 1 → 0 ms retry 1 → 250 ms retry 2 → 750 ms retry 3 → 1 500 ms retry 4 → 2 500 ms …Unlike exponential, this reads the previous delay, so the curve depends on the retry loop feeding each delay back in — which
StreamRetryProcessordoes.Parameter semantics match exponential.
-
linear
final StreamRetryPolicy linear(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long backoffStepMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)
Creates a linear back-off policy.
Delay increases by a constant backoffStepMillis each retry, capped at maxBackoffMillis.
Example with defaults:
attempt 1 → 0 ms retry 1 → 250 ms retry 2 → 500 ms retry 3 → 750 ms …Parameter semantics match exponential.
-
fixed
final StreamRetryPolicy fixed(@IntRange(from = 1.toLong()) Integer minRetries, @IntRange(from = 1.toLong()) Integer maxRetries, @IntRange(from = 0.toLong()) Long delayMillis, @IntRange(from = 0.toLong()) Long maxBackoffMillis, @IntRange(from = 0.toLong()) Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp)
Fixed-delay back-off: every retry waits exactly delayMillis, clamped to maxBackoffMillis for safety.
attempt 1 → 0 ms retry 1 → delayMillis retry 2 → delayMillis …
-
custom
final StreamRetryPolicy custom(Integer minRetries, Integer maxRetries, Long minBackoffMills, Long maxBackoffMills, Long initialDelayMillis, Function2<Integer, Throwable, Boolean> giveUp, Function2<Integer, Long, Long> nextDelay)
Custom builder – explicitly specify every parameter while still benefiting from the invariant checks in the private constructor.
Example:
val jitterPolicy = StreamRetryPolicy.custom( minRetries = 2, maxRetries = 6, minBackoffMills = 200, maxBackoffMills = 10_000, initialDelayMillis = 100, giveUp = { _, cause -> cause is AuthenticationException }, ) { attempt, prev -> val base = prev * 2 // add ±20 % jitter val jitter = (base * Random.nextDouble(0.8, 1.2)).toLong() jitter }- Parameters:
nextDelay- Lambda receives 1-based retry index and previous delay (orinitialDelayMillisfor the first retry) and must return the next delay in ms.
-
-
-
-