• Stars
    star
    9
  • Rank 1,939,727 (Top 39 %)
  • Language
    Scala
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically wrap Scala classes that return Futures with a Circuit Breaker

autobreaker

autobreaker is a Scala library that wraps your objects and intercepts all methods returning Futures with a circuit breaker.

It is based on atmos and Akka's Circuit Breaker, using Java's Proxy to intercept method calls.

Usage

import com.unstablebuild.autobreaker._

trait MyService {
  def add(a: Int, b: Int): Future[Int]
}

class FailingService extends MyService {
  override def add(a: Int, b: Int): Future[Int] = Future.failed(new Exception("error"))
}

val realService: MyService = new FailingService
val serviceWithCircuitBreaker = AutoBreaker.proxy(realService)

// Make it fail a few times
(1 to 10).foreach { _ => serviceWithCircuitBreaker.add(11, 23) }

// Try again and see that the service isn't called
serviceWithCircuitBreaker.add(11, 23)
// [warn] e.c.CircuitBreakerProxy - Attempt 1 of operation interrupted: akka.pattern.CircuitBreakerOpenException: Circuit Breaker is open; calls are failing fast
// akka.pattern.CircuitBreakerOpenException: Circuit Breaker is open; calls are failing fast

Please check the unit tests for more examples.

Skipping a specific method

You can prevent a method from being intercept with the NoCircuitBreaker annotation. For example:

class AnotherImplementation extends MyService {
  @NoCircuitBreaker
  override def add(a: Int, b: Int): Future[Int] = Future.failed(new Exception("error"))
}

Install

To use it with SBT, add the following to your build.sbt file:

resolvers += Resolver.sonatypeRepo("public")

libraryDependencies += "com.unstablebuild" %% "autobreaker" % "0.5.6"

Configuration

The following settings (and their default values) are available:

case class CircuitBreakerSettings(
  totalAttempts: TerminationPolicy = 3.attempts,
  backoffPolicy: BackoffPolicy = LimitedExponentialBackoffPolicy(2.minutes, 1.second),
  maxFailures: Int = 5,
  callTimeout: FiniteDuration = 10.seconds,
  resetTimeout: FiniteDuration = 1.minute,
  knownError: Throwable => Boolean = _ => false
)

Please see atmos and akka documentations for further reference.

knownError is used to decide if, given an exception type returned by the method, it should be retried or not, or counted as a failure on the circuit breaker. This allows the usage of custom exceptions to communicate the users about errors that don't affect the used method. For instance, you can decide that an exception communicating validation issues should not be considered as bad as a failure when communicating with a downstream system.

Guice Integration

autobreaker-guice allows you to annotate your implementations with @WithCircuitBreaker and modify your Guice module to automatically wrap the generated objects.

In order to use it, you need to add the following to your dependencies:

libraryDependencies += "com.unstablebuild" %% "autobreaker-guice" % "0.5.6"

Afterwards, it can be used like this:

@WithCircuitBreaker
class DownstreamService extends MyService {
  override def add(a: Int, b: Int): Future[Int] = ???
}

class TestModule extends AbstractModule {
  override def configure(): Unit = bind(classOf[MyService]).to(classOf[DownstreamService])
}

val module = new TestModule()
val injector = Guice.createInjector(AutoBreakerGuice.prepare(module))
val service = injector.getInstance(classOf[MyService])

An ExecutionContext and Scheduler should be available on your bindings.

You can also use a AutoBreakerModule instead of AbstractModule, and that will call AutoBreakerGuice.prepare for you.

Settings

You can inject settings in two ways:

  1. Bind a CircuitBreakerSettings;
  2. Bind a named CircuitBreakerSettings.

For example:

bind(classOf[CircuitBreakerSettings])
          .toInstance(mySettings)

bind(classOf[CircuitBreakerSettings])
  .annotatedWith(Names.named("custom-conf"))
  .toInstance(mySettings)

To use named Settings, you can add the use named to the WithCircuitBreaker annotation:

@WithCircuitBreaker(name = "custom-conf")
class CustomNamedFailureTestService(error: Throwable) extends MyService { ... }

If a Settings instance with the given name is not found, the library will try to use an unnamed one. If it also can't fidn one, it will fall back to AutoBreaker.defaultSettings.

Release

./sbt test guice/test
./sbt publishSigned guice/publishSigned
./sbt sonatypeReleaseAll

More Repositories

1

idid

A common interface for different Id types
Scala
14
star
2

coapex

CoAP protocol in Elixir
Elixir
13
star
3

settler

boilerplate-free typed settings generation in Scala
Scala
6
star
4

SuperMarket

Agile Brazil 2011 presentation content
Java
5
star
5

akka-cluster-examples

A few examples using akka-cluster
Scala
4
star
6

ircbot

simple bot that connects to a IRC room and save the transcripts to a txt file
Scala
4
star
7

draw-diagram

Generate Syntax Diagrams from simplified BNF rules
JavaScript
4
star
8

Mendeley-Importer-for-Safari

A simple button to let you import references and documents to you Mendeley Library with a single click.
JavaScript
4
star
9

s3mp

Simple Serial Slave-Master Protocol (S3MP)
4
star
10

reindxr

automatically (re)index files inside a folder using Apache Lucene
Scala
3
star
11

ditado

Distributed Issue Tracker
Ruby
3
star
12

nginx-route-testing

Test routes declared on your nginx configuration
Python
3
star
13

query-parser

parse advanced search queries using antlr4
JavaScript
2
star
14

filesyncher

Scala
2
star
15

scala-groovy_presentation

JavaScript
2
star
16

TicTacToe

game
Java
2
star
17

scala-jsonr

A very simple library to create JSON strings in Scala
Scala
2
star
18

golang-sks

Simple Key Store
Go
2
star
19

jcmph

Small experiment to crete a Java/Scala API to CMPH (C Minimal Perfect Hashing) Library.
C
2
star
20

code-an

Git/Jira code analyser
Scala
2
star
21

ScalaMines

The Mines game made in Scala
Scala
2
star
22

distributedHappyNumberFinder

Scala
1
star
23

dotfiles-control

Shell
1
star
24

irclog-dist

crazy, but works: it packs irclog, reindxr and ircbot all together
Shell
1
star
25

mapmytweets

Show in a map the tweets of a given account.
Ruby
1
star
26

scala-freenode-search

search the freenode scala channel history
Scala
1
star
27

code-an-gui

JavaScript
1
star
28

elixir-mines

minesweeper in elixir
Elixir
1
star
29

latest-build

JavaScript
1
star
30

pot

Page Object Tester
Ruby
1
star
31

dotfiles

Shell
1
star
32

ReflectionSpike

small experiences with java reflection
Java
1
star
33

theses-search

theses-search
JavaScript
1
star
34

irclog

play application to display content indexed by reindxr
Scala
1
star
35

client-ui

experiments with angularjs, grunt, bower, etc and how can they be applied on our project
JavaScript
1
star
36

moca

a distributed crawler capable of rendering pages with javascript
Scala
1
star
37

view-logs

a silly webserver to return log files in crystal
Crystal
1
star
38

brew-cask-explorer

explore the homepage of packages in homebrew-cask
JavaScript
1
star
39

scala-function-pipeline

A parallel pipeline of Scala functions
Scala
1
star
40

capitalism_with_zmq

ZMQ examples
Ruby
1
star
41

grooveshark-lyrics-safari

safari plugin for http://nettofarah.github.com/grooveshark-lyrics/
JavaScript
1
star
42

dotvim

.vim files
Vim Script
1
star
43

ScalatraInGAE

base project to run scalatra in Google App Engine [need to modify appengine-web.xml]
1
star
44

grooveshark-lyrics

spike to get the lyrics for the current song on grooveshark
JavaScript
1
star
45

rust-bananapi-dht11

A DHT11 driver using Rust for Banana Pi
Rust
1
star
46

textmate_theme

My TextMate theme
1
star
47

1st_Scala

experiences with Scala, ScalaTest and ScalaIDE
Scala
1
star
48

analytics-with-spark

Exercises for the book "Advanced Analytics with Spark"
Shell
1
star
49

mifare-card-reader

Access control using a gumstix board and a Mifare card reader. The access log is accessible through a web interface
C
1
star
50

torri.co

personal website
JavaScript
1
star
51

WebScalaMines

The ScalaMines, but now at your browser
JavaScript
1
star