Skip to content

Commit de14d85

Browse files
committed
Implement Retry functionality
1 parent d5a3d4c commit de14d85

7 files changed

Lines changed: 1098 additions & 7 deletions

File tree

core/shared/src/main/scala/cats/effect/IO.scala

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,17 @@ import cats.data.Ior
4040
import cats.effect.instances.spawn
4141
import cats.effect.kernel.CancelScope
4242
import cats.effect.kernel.GenTemporal.handleDuration
43-
import cats.effect.std.{Backpressure, Console, Env, Supervisor, UUIDGen}
43+
import cats.effect.std.{Backpressure, Console, Env, Retry, Supervisor, UUIDGen}
4444
import cats.effect.tracing.{Tracing, TracingEvent}
4545
import cats.effect.unsafe.IORuntime
46-
import cats.syntax._
47-
import cats.syntax.all._
46+
import cats.syntax.*
47+
import cats.syntax.all.*
4848

4949
import scala.annotation.unchecked.uncheckedVariance
50-
import scala.concurrent._
51-
import scala.concurrent.duration._
50+
import scala.concurrent.*
51+
import scala.concurrent.duration.*
5252
import scala.util.{Failure, Success, Try}
5353
import scala.util.control.NonFatal
54-
5554
import java.util.UUID
5655
import java.util.concurrent.Executor
5756

@@ -632,6 +631,45 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
632631
(FiberIO[A @uncheckedVariance], OutcomeIO[B])]] =
633632
IO.racePair(this, that)
634633

634+
/**
635+
* Evaluates the current IO with the given retry `policy`.
636+
*
637+
* @example
638+
* {{{
639+
* val policy = Retry.exponentialBackoff[IO, Throwable](1.second).withMaxRetries(10)
640+
* io.retry(policy)
641+
* }}}
642+
*
643+
* @param policy
644+
* the policy to use
645+
*/
646+
def retry(policy: Retry[IO, Throwable]): IO[A] =
647+
Retry.retry(policy)(this)
648+
649+
/**
650+
* Evaluates the current IO with the given retry `policy`.
651+
*
652+
* @example
653+
* {{{
654+
* val policy = Retry.exponentialBackoff[IO, Throwable](1.second).withMaxRetries(10)
655+
* io.retry(
656+
* policy,
657+
* (status, err, decision) => IO.println(s"Attempt ${status.retriesTotal}, error: ${err.getMessage}, next: $decision")
658+
* )
659+
* }}}
660+
*
661+
* @param policy
662+
* the policy to use
663+
*
664+
* @param onRetry
665+
* the effect to invoke on every retry decision
666+
*/
667+
def retry(
668+
policy: Retry[IO, Throwable],
669+
onRetry: (Retry.Status, Throwable, Retry.Decision) => IO[Unit]
670+
): IO[A] =
671+
Retry.retry(policy, onRetry)(this)
672+
635673
/**
636674
* Inverse of `attempt`
637675
*

core/shared/src/main/scala/cats/effect/syntax/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ package object syntax {
2929
object clock extends kernel.syntax.ClockSyntax
3030

3131
object supervisor extends std.syntax.SupervisorSyntax
32+
object retry extends std.syntax.RetrySyntax
3233
object dispatcher extends DispatcherSyntax
3334
}

0 commit comments

Comments
 (0)