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
- Fork it ( https://github.com/wooga/circuit_breaker/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request