2017-02-28 4 views
2

完全にシンプルに見えるakka httpクライアント上のサーキットブレーカの例は、私にとっては期待できません。httpクライアント上のサーキットブレーカは、障害後に閉じることができません。

object HttpWithCircuitBreaker extends App { 
    implicit val system = ActorSystem() 
    implicit val materializer = ActorMaterializer() 
    implicit val ec = system.dispatcher 
    val breaker = 
    new CircuitBreaker(
     system.scheduler, 
     maxFailures = 2, 
     callTimeout = 3.seconds, 
     resetTimeout = 25.seconds) 
     .onOpen(println("circuit breaker opened")) 
     .onClose(println("circuit breaker closed")) 
     .onHalfOpen(println("circuit breaker half-open")) 
    while (true) { 
    val futureResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "https://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new")) 
    breaker.withCircuitBreaker(futureResponse).map(resp => resp.status match { 
     case Success(_) => 
     resp.entity.dataBytes.runWith(Sink.ignore) 
     println("http success") 
     case _ => 
     resp.entity.dataBytes.runWith(Sink.ignore) 
     println(s"http error ${resp.status.intValue()}") 
    }).recover { 
     case [email protected]_ => 
     println(s"exception ${e.getMessage}") 
    } 
    Thread.sleep(1000) 
    } 
} 

乱数を完全に細かく取得し始めます。ネットから切断したときに開きますが、再接続すると閉じます。あなたはログから見ることができるようにそこに回復しようとする試みがあるが、それは、タイムアウトで失敗します。

http success 
http success 
http success 
exception Tcp command [Connect(www.random.org:443,None,List(),Some(10 seconds),true)] failed 
exception Circuit Breaker Timed out. 
circuit breaker opened 
exception Tcp command [Connect(www.random.org:443,None,List(),Some(10 seconds),true)] failed 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker Timed out. 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
circuit breaker half-open    <--- new http call should start here 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker Timed out. <--- but it fails with timeout 
circuit breaker opened 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 
exception Circuit Breaker is open; calls are failing fast 

直感は、それが何か未消費のHTTPエンティティでなければならないことを述べているが、それでも私はそれを右に作ることができませんよ。正しく動作する遮断器の

Here is the sample on github to play with

答えて

2

、このval

val futureResponse: Future[HttpResponse] = Http().singleRequest(...) 

は、回路ブレーカが非同期呼び出しをラップする必要がdef

def futureResponse: Future[HttpResponse] = Http().singleRequest(...) 

する必要があります - あなたの場合valを使用すると、非同期呼び出しがブレーカー外で開始されます。

あなたのコードにThread.sleepを使用しないようにしてください。 - Akka after

+0

ありがとうございました。ついにそれが分かりました。 – kharole

関連する問題