GithubHelp home page GithubHelp logo

failurewall's Introduction

Failurewall

This is a library to protect applications against failures, and helpful in developing stable, responsive and resilient systems.

Failurewall is inspired by Hystrix and adapted for scala.concurrent.Future.

Getting Started

You should add the following dependency.

libraryDependencies += "com.okumin" %% "failurewall-core" % "0.5.0"

If you are using Akka 2.4, you can use failurewall-akka. failurewall-akka provides the following failurewalls.

  • circuit breaker
  • retry with backoff
  • timeout
libraryDependencies += "com.okumin" %% "failurewall-akka" % "0.5.0"

If you are using Akka 2.3, see also failurewall-akka23.

How to use

As a Proxy

Failurewall is simple to use, wrapping scala.concurrent.Future to be protected. Each failurewall has abilities to handle failures.

object HttpClient {
  def get(url: String): Future[Response] = ???
}

val wall: Failurewall[Response, Response] = ???
val response: Future[Response] = wall.call(HttpClient.get("http://okumin.com/"))

Composability

Failurewalls has their own ability, e.g. retrying, checking rate limits and throttling. Failurewall#compose makes Failurewalls decorate such features.

val wallSina: Failurewall[Int, String] = ???
val wallRose: Failurewall[Int, Int] = ???
val wallMaria: Failurewall[Double, Int] = ???

val walls: [Double, String] = wallSina compose wallRose compose wallMaria

Built-in walls(failurewall-core)

Retries on temporary failures.

val wall = RetryFailurewall[Response](10, executionContext)
wall.call(Future.failed(new RuntimeException)) // retry 10 times

Keeps resource usage constant.

val wall = StdSemaphoreFailurewall[Response](10, executionContext)
val results = (1 to 100).map { _ =>
  // fails immediately while other 10 calls are running
  wall.call(doSomeOperation())
}

Measures the execution time.

val wall = StopwatchFailurewall[Response](executionContext)
wall.call(doSomeOperation()) // returns Future[(Try[Response], FiniteDuration)]

Built-in walls(failurewall-akka)

Prevents a failure from leading to cascading other failures.

// has a little complicated constructor
val wall: AkkaCircuitBreakerFailurewall[Response] = ???
val results = (1 to 100).map { _ =>
  // fail-fast after failure times exceeds the threshold
  wall.call(Future {
    throw new RuntimeException
  })
}

Retries with backoff on temporary failures.

val backoffStrategy = ExponentialBackoffStrategy(
  minBackoff = 100.millis,
  maxBackoff = 10.seconds,
  multiplier = 2.0
)
val wall = AkkaRetryFailurewall[Response](
  10,
  backoffStrategy,
  akkaScheduler,
  executionContext
)
// retry 10 times with exponential backoff
wall.call(Future.failed(new RuntimeException))

Times out when it takes some duration.

val wall = AkkaTimeoutFailurewall[String](5.seconds, akkaScheduler, executionContext) {
  logger.error("Timed out.")
}
// fails with FailurewallException
wall.call(Future {
  Thread.sleep(10000)
  "mofu"
})

failurewall's People

Contributors

dependabot[bot] avatar okumin avatar scala-steward-bot avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

failurewall's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.