All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.finaglecircuit.CircuitBreaking.scala Maven / Gradle / Ivy

package io.github.finaglecircuit

import com.twitter.finagle.http.Status._
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future

/**
 * An HTTP filter which adds circuit breaking to any service. CAn be used on service or client side.
 * Converts unexpected exceptions and broken circuit problems into 503 errors to the upstream client.
 * @param circuitBreaker the breaker to use
 */
class CircuitBreaking(circuitBreaker: CircuitBreaker) extends SimpleFilter[Request, Response] {

  override def apply(request: Request, next: Service[Request, Response]): Future[Response] = {
    circuitBreaker.withCircuit[Response](next.apply(request)).handle {
      case e: CircuitBroken => {
        val response = Response(ServiceUnavailable)
        response.setContentString(s"Call to ${circuitBreaker.name} failed: ${e.reason}")
        response
      }
      case e: Exception => {
        val response = Response(ServiceUnavailable)
        response.setContentString(s"Call to ${circuitBreaker.name} failed: ${e.getMessage}")
        response
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy