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

japgolly.webapputil.general.Retries.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC12
Show newest version
package japgolly.webapputil.general

import japgolly.microlibs.stdlib_ext.StdlibExt._
import java.time.Duration
import scala.collection.View

/** Immutable retry policy */
final case class Retries(waitTimes: Iterable[Duration]) {

  def apply(attemptsSoFar: Int): Option[Duration] =
    waitTimes.drop(attemptsSoFar).headOption

  def isEmpty: Boolean =
    waitTimes.isEmpty

  def take(n: Int): Retries =
    Retries(waitTimes.take(n))

  def takeWhile(f: Duration => Boolean): Retries =
    Retries(waitTimes.takeWhile(f))

  def pop: Option[(Duration, Retries)] =
    if (isEmpty)
      None
    else
      Some((waitTimes.head, Retries(waitTimes.tail)))

  def ++(r: Retries): Retries =
    Retries(waitTimes ++ r.waitTimes)
}

object Retries {
  private def expStream(d: Duration, factor: Double): LazyList[Duration] =
    d #:: expStream((d.toMillis * factor).millis, factor)

  def exponentially(d: Duration, factor: Double = 2): Retries =
    apply(expStream(d, factor))

  def continually(d: Duration): Retries =
    apply(View.from(Iterator.continually(d)))

  def none: Retries =
    Retries(Nil)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy