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 de14d85
Show file tree
Hide file tree
Showing 7 changed files with 1,098 additions and 7 deletions.
50 changes: 44 additions & 6 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ 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._
import cats.syntax.all._
import cats.syntax.*
import cats.syntax.all.*

import scala.annotation.unchecked.uncheckedVariance
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.*
import scala.concurrent.duration.*
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

import java.util.UUID
import java.util.concurrent.Executor

Expand Down Expand Up @@ -632,6 +631,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 de14d85

Please sign in to comment.