Getting started

Let’s start with a realistic example.

In order to provide business value to our stakeholders, we need to download a textual description of a cat gif.

Unfortunately we have to do this over a flaky network connection, so there’s a high probability it will fail.

We’ll be working with the cats-effect IO monad, but any monad will do.

import cats.effect.IO

val httpClient = util.FlakyHttpClient()
// httpClient: util.FlakyHttpClient = FlakyHttpClient()

val flakyRequest: IO[String] = IO {
  httpClient.getCatGif()
}
// flakyRequest: IO[String] = Delay(<function0>)

To improve the chance of successfully downloading the file, let’s wrap this with some retry logic.

We’ll add dependencies on the core and cats-effect modules:

val catsRetryVersion = "2.1.0"
libraryDependencies += "com.github.cb372" %% "cats-retry" % catsRetryVersion,

(Note: if you’re using Scala.js, you’ll need a %%% instead of %%.)

First we’ll need a retry policy. We’ll keep it simple: retry up to 5 times, with no delay between attempts. (See the retry policies page for information on more powerful policies).

import retry._

val retryFiveTimes = RetryPolicies.limitRetries[IO](5)
// retryFiveTimes: RetryPolicy[IO] = RetryPolicy(
//   retry.RetryPolicy$$$Lambda$11373/1404634182@20b1d476
// )

We’ll also provide an error handler that does some logging before every retry. Note how this also happens within whatever monad you’re working in, in this case the IO monad.

import cats.effect.IO
import scala.concurrent.duration.FiniteDuration
import retry._
import retry.RetryDetails._

val httpClient = util.FlakyHttpClient()
// httpClient: util.FlakyHttpClient = FlakyHttpClient()

val flakyRequest: IO[String] = IO {
  httpClient.getCatGif()
}
// flakyRequest: IO[String] = Delay(<function0>)

val logMessages = collection.mutable.ArrayBuffer.empty[String]
// logMessages: collection.mutable.ArrayBuffer[String] = ArrayBuffer(
//   "Failed to download. So far we have retried 0 times.",
//   "Failed to download. So far we have retried 1 times.",
//   "Failed to download. So far we have retried 2 times.",
//   "Failed to download. So far we have retried 3 times."
// )

def logError(err: Throwable, details: RetryDetails): IO[Unit] = details match {

  case WillDelayAndRetry(nextDelay: FiniteDuration,
                         retriesSoFar: Int,
                         cumulativeDelay: FiniteDuration) =>
    IO {
      logMessages.append(
        s"Failed to download. So far we have retried $retriesSoFar times.")
    }

  case GivingUp(totalRetries: Int, totalDelay: FiniteDuration) =>
    IO {
      logMessages.append(s"Giving up after $totalRetries retries")
    }

}

// Now we have a retry policy and an error handler, we can wrap our `IO` inretries.

// We need an implicit cats.effect.Timer
import cats.effect.Timer
import scala.concurrent.ExecutionContext.global
implicit val timer: Timer[IO] = IO.timer(global)
// timer: Timer[IO] = cats.effect.internals.IOTimer@3c003ec0


val flakyRequestWithRetry: IO[String] =
  retryingOnAllErrors[String](
    policy = RetryPolicies.limitRetries[IO](5),
    onError = logError
  )(flakyRequest)
// flakyRequestWithRetry: IO[String] = Bind(
//   Bind(
//     Bind(Delay(<function0>), <function1>, null),
//     retry.package$RetryingOnSomeErrorsPartiallyApplied$$Lambda$11782/1186468274@73267697,
//     StackTrace(
//       List(
//         cats.effect.internals.IOTracing$.buildFrame(IOTracing.scala:48),
//         cats.effect.internals.IOTracing$.buildCachedFrame(IOTracing.scala:39),
//         cats.effect.internals.IOTracing$.cached(IOTracing.scala:34),
//         cats.effect.IO.flatMap(IO.scala:133),
//         cats.effect.IOLowPriorityInstances$IOEffect.flatMap(IO.scala:886),
//         cats.effect.IOLowPriorityInstances$IOEffect.flatMap(IO.scala:863),
//         cats.FlatMap$Ops.flatMap(FlatMap.scala:229),
//         cats.FlatMap$Ops.flatMap$(FlatMap.scala:229),
//         cats.FlatMap$ToFlatMapOps$$anon$2.flatMap(FlatMap.scala:243),
//         retry.package$RetryingOnSomeErrorsPartiallyApplied.$anonfun$apply$3(package.scala:97),
//         cats.StackSafeMonad.tailRecM(StackSafeMonad.scala:16),
//         cats.StackSafeMonad.tailRecM$(StackSafeMonad.scala:15),
//         cats.effect.IOLowPriorityInstances$IOEffect.tailRecM(IO.scala:863),
//         retry.package$RetryingOnSomeErrorsPartiallyApplied.apply(package.scala:96),
//         repl.Session$App.<init>(combinators.md:65),
//         repl.Session$.app(combinators.md:3),
//         mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$2(DocumentBuilder.scala:82),
//         scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23),
//         scala.util.DynamicVariable.withValue(DynamicVariable.scala:62),
//         scala.Console$.withErr(Console.scala:196),
//         mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$1(DocumentBuilder.scala:82),
//         scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23),
//         scala.util.DynamicVariable.withValue(DynamicVariable.scala:62),
//         scala.Console$.withOut(Console.scala:167),
//         mdoc.internal.document.DocumentBuilder$$doc$.build(DocumentBuilder.scala:81),
//         mdoc.internal.markdown.MarkdownCompiler$.buildDocument(MarkdownCompiler.scala:53),
//         mdoc.internal.markdown.Processor.processScalaInputs(Processor.scala:133),
//         mdoc.internal.markdown.Processor.processDocument(Processor.scala:48),
//         mdoc.internal.markdown.Markdown$.toMarkdown(Markdown.scala:118),
//         mdoc.internal.cli.MainOps.handleMarkdown(MainOps.scala:62),
//         mdoc.internal.cli.MainOps.handleFile(MainOps.scala:90),
//         mdoc.internal.cli.MainOps.$anonfun$generateCompleteSite$1(MainOps.scala:130),
//         scala.collection.LinearSeqOptimized.foldLeft(LinearSeqOptimized.scala:126),
// ...

// Let's see it in action.

flakyRequestWithRetry.unsafeRunSync()
// res2: String = "cute cat gets sleepy and falls asleep"

logMessages.foreach(println)
// Failed to download. So far we have retried 0 times.
// Failed to download. So far we have retried 1 times.
// Failed to download. So far we have retried 2 times.
// Failed to download. So far we have retried 3 times.

Next steps: