Package 

Object StreamRetryPolicy.Companion

    • 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 × backoffStepMillis

        then clamped to [backoffStepMillis, maxBackoffMillis]. Driven from an initialDelayMillis of 0, 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, 1000 against exponential's 100, 200, 400, 800; doubling only overtakes it at retry 5, and with the default maxRetries = 5 the 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 StreamRetryProcessor does.

        Parameter semantics match exponential.

      • 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 (or initialDelayMillis for the first retry) and must return the next delay in ms.