Skip to content

Commit

Permalink
Implement Retry functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Jul 26, 2024
1 parent d5a3d4c commit c1fea11
Show file tree
Hide file tree
Showing 7 changed files with 1,094 additions and 2 deletions.
41 changes: 40 additions & 1 deletion core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import cats.data.Ior
import cats.effect.instances.spawn
import cats.effect.kernel.CancelScope
import cats.effect.kernel.GenTemporal.handleDuration
import cats.effect.std.{Backpressure, Console, Env, Supervisor, UUIDGen}
import cats.effect.std.{Backpressure, Console, Env, Retry, Supervisor, UUIDGen}
import cats.effect.tracing.{Tracing, TracingEvent}
import cats.effect.unsafe.IORuntime
import cats.syntax._
Expand Down Expand Up @@ -632,6 +632,45 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
(FiberIO[A @uncheckedVariance], OutcomeIO[B])]] =
IO.racePair(this, that)

/**
* Evaluates the current IO with the given retry `policy`.
*
* @example
* {{{
* val policy = Retry.exponentialBackoff[IO, Throwable](1.second).withMaxRetries(10)
* io.retry(policy)
* }}}
*
* @param policy
* the policy to use
*/
def retry(policy: Retry[IO, Throwable]): IO[A] =
Retry.retry(policy)(this)

/**
* Evaluates the current IO with the given retry `policy`.
*
* @example
* {{{
* val policy = Retry.exponentialBackoff[IO, Throwable](1.second).withMaxRetries(10)
* io.retry(
* policy,
* (status, err, decision) => IO.println(s"Attempt $${status.retriesTotal}, error: $${err.getMessage}, next: $$decision")
* )
* }}}
*
* @param policy
* the policy to use
*
* @param onRetry
* the effect to invoke on every retry decision
*/
def retry(
policy: Retry[IO, Throwable],
onRetry: (Retry.Status, Throwable, Retry.Decision) => IO[Unit]
): IO[A] =
Retry.retry(policy, onRetry)(this)

/**
* Inverse of `attempt`
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ package object syntax {
object clock extends kernel.syntax.ClockSyntax

object supervisor extends std.syntax.SupervisorSyntax
object retry extends std.syntax.RetrySyntax
object dispatcher extends DispatcherSyntax
}
Loading

0 comments on commit c1fea11

Please sign in to comment.