All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.valaphee.foundry.retry.policy.Delay.kt Maven / Gradle / Ivy

/*
 * Copyright (c) 2021, Valaphee.
 * All rights reserved.
 */

package com.valaphee.foundry.retry.policy

import com.valaphee.foundry.retry.ContinueRetrying
import com.valaphee.foundry.retry.StopRetrying
import com.valaphee.foundry.retry.retryAfter
import kotlin.math.min

fun constantDelay(delay: Long): RetryPolicy<*> {
    require(delay > 0) { "delay must be positive: $delay" }

    return { retryAfter(delay) }
}

fun  RetryPolicy.maxDelay(delay: Long): RetryPolicy {
    require(delay > 0) { "delay must be positive: $delay" }

    return {
        when (val instruction = this.(this@maxDelay)()) {
            StopRetrying, ContinueRetrying -> instruction
            else -> retryAfter(min(instruction.delay, delay))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy