Skip to content

Instantly share code, notes, and snippets.

@viktorklang
Last active July 23, 2023 23:48

Revisions

  1. viktorklang revised this gist Mar 14, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions Future-retry.scala
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,7 @@ import akka.actor.Scheduler
    /**
    * Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
    * in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
    * the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain a TimeoutException.
    * the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
    **/
    def retry[T](op: => T, delay: FiniteDuration, retries: Int)(implicit ec: ExecutionContext, s: Scheduler): Future[T] =
    if (retries < 0) Future.failed(new TimeoutException)
    else Future(op) recoverWith { case _ => after(delay, s)(retry(op, delay, retries - 1)) }
    Future(op) recoverWith { case _ if retries > 0 => after(delay, s)(retry(op, delay, retries - 1)) }
  2. viktorklang revised this gist Mar 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Future-retry.scala
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ import akka.actor.Scheduler
    * Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
    * in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
    * the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain a TimeoutException.
    * /
    **/
    def retry[T](op: => T, delay: FiniteDuration, retries: Int)(implicit ec: ExecutionContext, s: Scheduler): Future[T] =
    if (retries < 0) Future.failed(new TimeoutException)
    else Future(op) recoverWith { case _ => after(delay, s)(retry(op, delay, retries - 1)) }
  3. viktorklang created this gist Mar 7, 2014.
    14 changes: 14 additions & 0 deletions Future-retry.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import scala.concurrent.duration._
    import scala.concurrent.ExecutionContext
    import scala.concurrent.Future
    import akka.pattern.after
    import akka.actor.Scheduler

    /**
    * Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
    * in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
    * the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain a TimeoutException.
    * /
    def retry[T](op: => T, delay: FiniteDuration, retries: Int)(implicit ec: ExecutionContext, s: Scheduler): Future[T] =
    if (retries < 0) Future.failed(new TimeoutException)
    else Future(op) recoverWith { case _ => after(delay, s)(retry(op, delay, retries - 1)) }