Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Retry functionality #4109

Open
wants to merge 5 commits into
base: series/3.x
Choose a base branch
from
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 @@ -647,6 +648,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
Loading