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

Max backoff for exponential backoffs #16

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions src/main/scala/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import java.util.concurrent.TimeUnit

object Defaults {
val delay: FiniteDuration = Duration(500, TimeUnit.MILLISECONDS)
val maxDelay: Duration = Duration.Inf
}

14 changes: 10 additions & 4 deletions src/main/scala/Policy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ object Pause {
}

object Backoff {
private def nextDelay(calculatedDelay: FiniteDuration, maxDelay: Duration): FiniteDuration =
maxDelay match {
case _: Duration.Infinite => calculatedDelay
case delay: FiniteDuration => List(calculatedDelay, delay).min
}

/** Retry with exponential backoff forever */
def forever(delay: FiniteDuration = Defaults.delay, base: Int = 2)
def forever(delay: FiniteDuration = Defaults.delay, base: Int = 2, maxDelay: Duration = Defaults.maxDelay)
(implicit timer: Timer): Policy =
new Policy {
def apply[T]
Expand All @@ -88,7 +93,7 @@ object Backoff {
executor: ExecutionContext): Future[T] = {
def run(delay: FiniteDuration): Future[T] = retry(promise, { () =>
Delay(delay) {
run(Duration(delay.length * base, delay.unit))
run(nextDelay(delay * base, maxDelay))
}.future.flatMap(identity)
})
run(delay)
Expand All @@ -99,7 +104,8 @@ object Backoff {
def apply(
max: Int = 8,
delay: FiniteDuration = Defaults.delay,
base: Int = 2)
base: Int = 2,
maxDelay: Duration = Defaults.maxDelay)
(implicit timer: Timer): Policy =
new CountingPolicy {
def apply[T]
Expand All @@ -109,7 +115,7 @@ object Backoff {
def run(max: Int, delay: FiniteDuration): Future[T] = countdown(
max, promise,
count => Delay(delay) {
run(count, Duration(delay.length * base, delay.unit))
run(count, nextDelay(delay * base, maxDelay))
}.future.flatMap(identity))
run(max, delay)
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/scala/PolicySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ class PolicySpec extends FunSpec with BeforeAndAfterAll {
assert(took <= 110.millis === true,
s"took more time than expected: $took")
}

it ("should wait up to a max time each time") {
implicit val success = Success[Int](_ == 2)
val tries = forwardCountingFutureStream().iterator
val policy = retry.Backoff(2, 30.millis, maxDelay = 50.millis)
val took = time {
val result = Await.result(policy(tries.next),
80.millis + 20.millis)
assert(success.predicate(result) === true, "predicate failed")
}
assert(took >= 80.millis === true,
s"took less time than expected: $took")
assert(took <= 100.millis === true,
s"took more time than expected: $took")
}
}

describe("retry.When") {
Expand Down