hipjim / scala-retry   0.4.0

Apache License 2.0 GitHub

Simple retry mechanism for arbitrary function calls in Scala.

Scala versions: 2.13 2.12 2.11 2.10

Scala Retry

Build Status

Simple retry mechanism for arbitrary function calls in scala.

We live in times where software systems are built by small services that are talking to each other. In such systems, transient errors can occur and interfere with the normal program execution flow. Transient faults are temporary abnormal conditions such as offline services, infrastructure faults, or network issues. It is common practice to retry transient faults using a retry loop mechanism. This minimalistic library tries to solve this problem by implementing the retry side effect and give the user different backoff strategies.

Maven artifacts

Maven Central

libraryDependencies += "com.github.hipjim" %% "scala-retry" % "0.4.0"
compile group: 'com.github.hipjim', name: 'scala-retry_2.13', version: '0.4.0'
<dependency>
  <groupId>com.github.hipjim</groupId>
  <artifactId>scala-retry_2.13</artifactId>
  <version>0.4.0</version>
</dependency>

Versions for Scala 2.12, Scala 2.11 and Scala 2.10 are available.

Tutorial

import scala.concurrent.duration._
import util.retry.blocking.{RetryStrategy, Failure, Retry, Success}

// define the retry strategy
implicit val retryStrategy =
    RetryStrategy.fixedBackOff(retryDuration = 1.seconds, maxAttempts = 2)

// pattern match the result
val r = Retry(1 / 1) match {
    case Success(x) => x
    case Failure(t) => log("I got 99 problems but you won't be one", t)
}

// recover in case of a failure
val recover = Retry(1 / 0) recover {
    case NonFatal(t) => Int.MaxValue
}

// get or else in case of failure
val result = Retry(1 / 0).getOrElse(1)

// can be used in for comprehensions
val result = for {
  x <- Retry(1 / 0) // fails with java.lang.ArithmeticException: / by zero
  y <- Retry(1 / 1) // success
} yield x + y // result is Failure with java.lang.ArithmeticException: / by zero

Retry strategies

Fixed backoff

val retryStrategy =
    RetryStrategy.fixedBackOff(retryDuration = 3.seconds, maxAttempts = 5)

Fibonacci backoff

val retryStrategy =
    RetryStrategy.fibonacciBackOff(initialWaitDuration = 3.seconds, maxAttempts = 5)

Random backoff

val retryStrategy =
    RetryStrategy.randomBackOff(
      minimumWaitDuration = 1.seconds, 
      maximumWaitDuration = 10.seconds, 
      maxAttempts = 10
    )

No backoff

val retryStrategy = RetryStrategy.noBackOff(maxAttempts = 10)

No retry

val retryStrategy = RetryStrategy.noRetry