Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added limitRetriersWithFixedDelay which is implemented in the docs #410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/core/shared/src/main/scala/retry/RetryPolicies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ object RetryPolicies {
show"limitRetries(maxRetries=$maxRetries)"
)

/** Retry up to maxRetries with fixed delay between retries
*/
def limitRetriesWithFixedDelay[M[_]: Applicative](
maxRetries: Int,
delay: FiniteDuration
): RetryPolicy[M] =
RetryPolicies
.limitRetries[M](maxRetries)
.join(RetryPolicies.constantDelay(delay))

/** Delay(n) = Delay(n - 2) + Delay(n - 1)
*
* e.g. if `baseDelay` is 10 milliseconds, the delays before each retry will be
Expand Down
23 changes: 23 additions & 0 deletions modules/core/shared/src/test/scala/retry/RetryPoliciesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class RetryPoliciesSpec extends AnyFlatSpec with Checkers {
s"exponentialBackoff($baseDelay)"
)
),
for {
delay <- genFiniteDuration
maxRetries <- Gen.posNum[Int]
} yield LabelledRetryPolicy(
limitRetriesWithFixedDelay(maxRetries, delay),
s"limitRetriesWithFixedDelay($maxRetries, $delay)"
),
Gen
.posNum[Int]
.map(maxRetries =>
Expand Down Expand Up @@ -198,6 +205,22 @@ class RetryPoliciesSpec extends AnyFlatSpec with Checkers {
}
}

behavior of "limitRetriesWithFixedDelay"

it should "always retry with the same delay or give up after maxRetries is reached" in check {
(status: RetryStatus) =>
val limit = 500
val verdict =
limitRetriesWithFixedDelay(limit, 1.millisecond).decideNextRetry(
status
)
if (status.retriesSoFar < limit) {
verdict == PolicyDecision.DelayAndRetry(1.millisecond)
} else {
verdict == PolicyDecision.GiveUp
}
}

behavior of "limitRetriesByDelay"

it should "give up if the underlying policy chooses a delay greater than the threshold" in {
Expand Down