Repository is archived
No commit activity in last 3 years
No release in over 3 years
Prevent long running external calls from blocking an application
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
 Project Readme

CircuitBreaker

Implementation of the circuit breaker pattern to protect against high latencies or outages of services. See Martin Fowlers writeup for more information.

There are other implementations by wsargent. and soundcloud.

Installation

Add this line to your application's Gemfile:

gem 'ya_circuit_breaker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ya_circuit_breaker

Usage

require 'circuit_breaker'

options = {
  # number of failures before the circuit breaker trips
  failure_threshold: 5,
  # invocation timeout in seconds
  invocation_timeout: 0.5,
  # a list of timeouts for consecutive failures in seconds. can be used for exponential backoff
  # each element can be also a Proc, that can operate on a number of retries after circuit breaker trips
  reset_timeouts: [2, 4, 8, 16, 32, 64, Proc.new {|retry| rand(retry * 10)}],
  # a list of errors or exceptions that indicates outtage of service
  errors_handled: [Redis::CommandError]
}

circuit_breaker = CircuitBreaker::Basic.new(options)

begin
  circuit_breaker.execute do
    http_api_call()
  end
rescue CircuitBreaker::CircuitBrokenError
  $stderr.puts "Circuit tripped"
rescue Timeout::Error
  $stderr.puts "Call took too long"
resque Redis::CommandError  # from errors_handled
  $stderr.puts "One of the errors indicating outtage of the service"
rescue Error
  $stderr.puts "Error thrown by 'http_api_call'"
end

Contributing

  1. Fork it ( https://github.com/wooga/circuit_breaker/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request