|
| 1 | +package ox.retry |
| 2 | + |
| 3 | +import scala.concurrent.duration.* |
| 4 | +import scala.util.Random |
| 5 | + |
| 6 | +private[retry] sealed trait Schedule: |
| 7 | + def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration |
| 8 | + |
| 9 | +object Schedule: |
| 10 | + |
| 11 | + private[retry] sealed trait Finite extends Schedule: |
| 12 | + def maxRetries: Int |
| 13 | + |
| 14 | + private[retry] sealed trait Infinite extends Schedule |
| 15 | + |
| 16 | + /** A schedule that retries up to a given number of times, with no delay between subsequent attempts. |
| 17 | + * |
| 18 | + * @param maxRetries |
| 19 | + * The maximum number of retries. |
| 20 | + */ |
| 21 | + case class Immediate(maxRetries: Int) extends Finite: |
| 22 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = Duration.Zero |
| 23 | + |
| 24 | + object Immediate: |
| 25 | + /** A schedule that retries indefinitely, with no delay between subsequent attempts. */ |
| 26 | + def forever: Infinite = ImmediateForever |
| 27 | + |
| 28 | + private case object ImmediateForever extends Infinite: |
| 29 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = Duration.Zero |
| 30 | + |
| 31 | + /** A schedule that retries up to a given number of times, with a fixed delay between subsequent attempts. |
| 32 | + * |
| 33 | + * @param maxRetries |
| 34 | + * The maximum number of retries. |
| 35 | + * @param delay |
| 36 | + * The delay between subsequent attempts. |
| 37 | + */ |
| 38 | + case class Delay(maxRetries: Int, delay: FiniteDuration) extends Finite: |
| 39 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = delay |
| 40 | + |
| 41 | + object Delay: |
| 42 | + /** A schedule that retries indefinitely, with a fixed delay between subsequent attempts. |
| 43 | + * |
| 44 | + * @param delay |
| 45 | + * The delay between subsequent attempts. |
| 46 | + */ |
| 47 | + def forever(delay: FiniteDuration): Infinite = DelayForever(delay) |
| 48 | + |
| 49 | + case class DelayForever private[retry] (delay: FiniteDuration) extends Infinite: |
| 50 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = delay |
| 51 | + |
| 52 | + /** A schedule that retries up to a given number of times, with an increasing delay (backoff) between subsequent attempts. |
| 53 | + * |
| 54 | + * The backoff is exponential with base 2 (i.e. the next delay is twice as long as the previous one), starting at the given initial delay |
| 55 | + * and capped at the given maximum delay. |
| 56 | + * |
| 57 | + * @param maxRetries |
| 58 | + * The maximum number of retries. |
| 59 | + * @param initialDelay |
| 60 | + * The delay before the first retry. |
| 61 | + * @param maxDelay |
| 62 | + * The maximum delay between subsequent retries. |
| 63 | + * @param jitter |
| 64 | + * A random factor used for calculating the delay between subsequent retries. See [[Jitter]] for more details. Defaults to no jitter, |
| 65 | + * i.e. an exponential backoff with no adjustments. |
| 66 | + */ |
| 67 | + case class Backoff( |
| 68 | + maxRetries: Int, |
| 69 | + initialDelay: FiniteDuration, |
| 70 | + maxDelay: FiniteDuration = 1.minute, |
| 71 | + jitter: Jitter = Jitter.None |
| 72 | + ) extends Finite: |
| 73 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = |
| 74 | + Backoff.nextDelay(attempt, initialDelay, maxDelay, jitter, lastDelay) |
| 75 | + |
| 76 | + object Backoff: |
| 77 | + private[retry] def delay(attempt: Int, initialDelay: FiniteDuration, maxDelay: FiniteDuration): FiniteDuration = |
| 78 | + // converting Duration <-> Long back and forth to avoid exceeding maximum duration |
| 79 | + (initialDelay.toMillis * Math.pow(2, attempt)).toLong.min(maxDelay.toMillis).millis |
| 80 | + |
| 81 | + private[retry] def nextDelay( |
| 82 | + attempt: Int, |
| 83 | + initialDelay: FiniteDuration, |
| 84 | + maxDelay: FiniteDuration, |
| 85 | + jitter: Jitter, |
| 86 | + lastDelay: Option[FiniteDuration] |
| 87 | + ): FiniteDuration = |
| 88 | + def backoffDelay = Backoff.delay(attempt, initialDelay, maxDelay) |
| 89 | + |
| 90 | + jitter match |
| 91 | + case Jitter.None => backoffDelay |
| 92 | + case Jitter.Full => Random.between(0, backoffDelay.toMillis).millis |
| 93 | + case Jitter.Equal => |
| 94 | + val backoff = backoffDelay.toMillis |
| 95 | + (backoff / 2 + Random.between(0, backoff / 2)).millis |
| 96 | + case Jitter.Decorrelated => |
| 97 | + val last = lastDelay.getOrElse(initialDelay).toMillis |
| 98 | + Random.between(initialDelay.toMillis, last * 3).millis |
| 99 | + |
| 100 | + /** A schedule that retries indefinitely, with an increasing delay (backoff) between subsequent attempts. |
| 101 | + * |
| 102 | + * The backoff is exponential with base 2 (i.e. the next delay is twice as long as the previous one), starting at the given initial |
| 103 | + * delay and capped at the given maximum delay. |
| 104 | + * |
| 105 | + * @param initialDelay |
| 106 | + * The delay before the first retry. |
| 107 | + * @param maxDelay |
| 108 | + * The maximum delay between subsequent retries. |
| 109 | + * @param jitter |
| 110 | + * A random factor used for calculating the delay between subsequent retries. See [[Jitter]] for more details. Defaults to no jitter, |
| 111 | + * i.e. an exponential backoff with no adjustments. |
| 112 | + */ |
| 113 | + def forever(initialDelay: FiniteDuration, maxDelay: FiniteDuration = 1.minute, jitter: Jitter = Jitter.None): Infinite = |
| 114 | + BackoffForever(initialDelay, maxDelay, jitter) |
| 115 | + |
| 116 | + case class BackoffForever private[retry] (initialDelay: FiniteDuration, maxDelay: FiniteDuration = 1.minute, jitter: Jitter = Jitter.None) |
| 117 | + extends Infinite: |
| 118 | + override def nextDelay(attempt: Int, lastDelay: Option[FiniteDuration]): FiniteDuration = |
| 119 | + Backoff.nextDelay(attempt, initialDelay, maxDelay, jitter, lastDelay) |
0 commit comments