Last active
January 24, 2022 19:48
-
-
Save jasongoodwin/40e90bbb329243176988 to your computer and use it in GitHub Desktop.
Akka CircuitBreaker Example
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
package com.akkademy | |
import akka.actor._ | |
import akka.event.Logging | |
import akka.pattern.{CircuitBreaker, ask} | |
import akka.util.Timeout | |
import com.akkademy.sapi.{GetRequest, KeyNotFoundException, SetRequest} | |
import scala.collection.mutable.HashMap | |
import scala.concurrent.Await | |
import scala.concurrent.duration._ | |
class FastSlowAkkademyDb extends Actor { | |
val map = new HashMap[String, Object] | |
val log = Logging(context.system, this) | |
override def receive = { | |
case SetRequest(key, value) => | |
log.info("received SetRequest - key: {} value: {}", key, value) | |
map.put(key, value) | |
sender() ! Status.Success | |
case GetRequest(key) => | |
Thread.sleep(70) | |
respondToGet(key) | |
case o => Status.Failure(new ClassNotFoundException) | |
} | |
def respondToGet(key: String): Unit = { | |
val response: Option[Object] = map.get(key) | |
response match { | |
case Some(x) => sender() ! x | |
case None => sender() ! Status.Failure(new KeyNotFoundException(key)) | |
} | |
} | |
} | |
object Main extends App { | |
val system = ActorSystem("Akkademy") | |
implicit val ec = system.dispatcher | |
val breaker = | |
new CircuitBreaker(system.scheduler, | |
maxFailures = 10, | |
callTimeout = 1 seconds, | |
resetTimeout = 1 seconds). | |
onOpen(println("circuit breaker opened!")). | |
onClose(println("circuit breaker closed!")). | |
onHalfOpen(println("circuit breaker half-open")) | |
implicit val timeout = Timeout(2 seconds) | |
val db = system.actorOf(Props[FastSlowAkkademyDb]) | |
Await.result(db ? SetRequest("key", "value"), 2 seconds) | |
(1 to 1000000).map(x => { | |
Thread.sleep(50) | |
val askFuture = breaker.withCircuitBreaker(db ? GetRequest("key")) | |
askFuture.map(x => "got it: " + x).recover({ | |
case t => "error: " + t.toString | |
}).foreach(x => println(x)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, thanks! :)