Skip to content

Instantly share code, notes, and snippets.

@stevebakh
Created March 7, 2018 09:49
Show Gist options
  • Select an option

  • Save stevebakh/52ca342f2b189eb1187b4f203accc362 to your computer and use it in GitHub Desktop.

Select an option

Save stevebakh/52ca342f2b189eb1187b4f203accc362 to your computer and use it in GitHub Desktop.
Basic Monix example of a circuit-breaker used on a task.
import monix.eval.{Task, TaskCircuitBreaker}
import monix.execution.Scheduler.Implicits.global
import scala.concurrent.duration.DurationDouble
import scala.util.{Failure, Success}
object Main extends App {
private val task = Task {
val nr = util.Random.nextInt()
if (nr % 2 == 0)
nr
else
throw new RuntimeException("This exception emulates a failure from the upstream service")
}
private val circuitBreaker = TaskCircuitBreaker(maxFailures = 5, resetTimeout = 10.seconds)
private val protectedTask: Task[Int] = circuitBreaker.protect(task)
while(true) {
println("running task...")
protectedTask.runOnComplete {
case Success(number) => println(s"successful number: $number")
case Failure(ex) => println(s"errrrror: ${ex.getMessage}")
}
Thread.sleep(1000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment