Skip to content

Commit

Permalink
Retry: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Jul 28, 2024
1 parent 20e73e3 commit 7f25d62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/Retry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ import scala.reflect.{classTag, ClassTag}
* Glossary:
* - individual delay - the delay between retries
* - cumulative delay - the total delay accumulated across all retries
*
* ==Usage==
*
* ===Retry on all errors===
*
* {{{
* val policy = Retry
* .exponentialBackoff[IO, Throwable](1.second)
* .withMaxRetries(10)
*
* // retries 10 times at most using an exponential backoff strategy
* IO.raiseError(new RuntimeException("oops")).retry(policy)
* }}}
*
* ===Retry on some errors (e.g. TimeoutException)===
*
* {{{
* val policy = Retry
* .exponentialBackoff[IO, Throwable](1.second)
* .withMaxRetries(10)
* .withErrorMatcher(Retry.ErrorMatcher[IO, Throwable].only[TimeoutException])
*
* // retries 10 times at most using an exponential backoff strategy
* IO.raiseError(new TimeoutException("timeout")).retry(policy)
*
* // gives up immediately
* IO.raiseError(new RuntimeException("oops")).retry(policy)
* }}}
*
* ===Retry on all errors except the TimeoutException===
*
* {{{
* val policy = Retry
* .exponentialBackoff[IO, Throwable](1.second)
* .withMaxRetries(10)
* .withErrorMatcher(Retry.ErrorMatcher[IO, Throwable].except[TimeoutException])
*
* // retries 10 times at most using an exponential backoff strategy
* IO.raiseError(new RuntimeException("oops")).retry(policy)
*
* // gives up immediately
* IO.raiseError(new TimeoutException("timeout")).retry(policy)
* }}}
*/
sealed trait Retry[F[_], E] {
import Retry.{Decision, ErrorMatcher, Status}
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/cats/effect/std/RetrySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ class RetrySpec extends BaseSpec {
result <- mtlRetry[F, Errors, Unit](
io,
policy,
(s, e: Errors, d) => EitherT.liftF(ref.update(_ :+ (s, d, e)))
(s, e: Errors, d) => EitherT.liftF(ref.update(_ :+ ((s, d, e))))
).value
attempts <- ref.get
} yield (result, attempts)
Expand Down Expand Up @@ -544,7 +544,7 @@ class RetrySpec extends BaseSpec {
result <- mtlRetry[F, Errors, Unit](
io,
policy,
(s, e: Errors, d) => EitherT.liftF(ref.update(_ :+ (s, d, e)))
(s, e: Errors, d) => EitherT.liftF(ref.update(_ :+ ((s, d, e))))
).value
attempts <- ref.get
} yield (result, attempts)
Expand Down

0 comments on commit 7f25d62

Please sign in to comment.