Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import cats.effect.std.{
Backpressure,
Console,
Env,
Retry,
SecureRandom,
Supervisor,
SystemProperties,
Expand Down Expand Up @@ -646,6 +647,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 onError
* the effect to invoke on every retry decision
*/
def retry(
policy: Retry[IO, Throwable],
onError: (Retry.Status, Throwable, Retry.Decision) => IO[Unit]
): IO[A] =
Retry.retry(policy, onError)(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