Created
March 7, 2018 09:49
-
-
Save stevebakh/52ca342f2b189eb1187b4f203accc362 to your computer and use it in GitHub Desktop.
Basic Monix example of a circuit-breaker used on a task.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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