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

add ability to combine Schedules for retries #129

Merged
Merged
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
22 changes: 22 additions & 0 deletions core/src/main/scala/ox/resilience/Schedule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ object Schedule:

private[resilience] sealed trait Finite extends Schedule:
def maxRetries: Int
def fallbackTo(fallback: Finite): Finite = FallingBack(this, fallback)
def fallbackTo(fallback: Infinite): Infinite = FallingBack.forever(this, fallback)

private[resilience] sealed trait Infinite extends Schedule

Expand Down Expand Up @@ -117,3 +119,23 @@ object Schedule:
extends Infinite:
override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration =
Backoff.nextDelay(attempt, initialDelay, maxDelay, jitter, lastDelay)

private[resilience] sealed trait WithFallback extends Schedule:
def base: Finite
def fallback: Schedule
override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration =
if base.maxRetries > attempt then base.nextDelay(attempt, lastDelay)
else fallback.nextDelay(attempt - base.maxRetries, lastDelay)

/** A schedule that combines two schedules, using [[base]] first [[base.maxRetries]] number of times, and then using [[fallback]]
* [[fallback.maxRetries]] number of times.
*/
case class FallingBack(base: Finite, fallback: Finite) extends WithFallback, Finite:
override def maxRetries: Int = base.maxRetries + fallback.maxRetries

object FallingBack:
/** A schedule that retries indefinitely, using [[base]] first [[base.maxRetries]] number of times, and then always using [[fallback]].
*/
def forever(base: Finite, fallback: Infinite): Infinite = FallingBackForever(base, fallback)

case class FallingBackForever private[resilience] (base: Finite, fallback: Infinite) extends WithFallback, Infinite
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ox.resilience

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import ox.ElapsedTime

import scala.concurrent.duration.*

class ScheduleFallingBackRetryTest extends AnyFlatSpec with Matchers with ElapsedTime:
behavior of "retry with combination of schedules"

it should "retry 3 times immediately and then 2 times with delay" in {
// given
var counter = 0
val sleep = 100.millis
val immediateRetries = 3
val delayedRetries = 2

def f =
counter += 1
throw new RuntimeException("boom")

val schedule = Schedule.Immediate(immediateRetries).fallbackTo(Schedule.Delay(delayedRetries, sleep))

// when
val (result, elapsedTime) = measure(the[RuntimeException] thrownBy retry(RetryPolicy(schedule))(f))

// then
result should have message "boom"
counter shouldBe immediateRetries + delayedRetries + 1
elapsedTime.toMillis should be >= 2 * sleep.toMillis
}

it should "retry forever" in {
// given
var counter = 0
val retriesUntilSuccess = 1_000
val successfulResult = 42

def f =
counter += 1
if counter <= retriesUntilSuccess then throw new RuntimeException("boom") else successfulResult

val schedule = Schedule.Immediate(100).fallbackTo(Schedule.Delay.forever(2.millis))

// when
val result = retry(RetryPolicy(schedule))(f)

// then
result shouldBe successfulResult
counter shouldBe retriesUntilSuccess + 1
}
Loading