rbsafecircuit
rbsafecircuit is a Ruby gem for implementing a circuit breaker pattern with event handling capabilities using event_emitter
.
Installation
Add this line to your application's Gemfile:
gem 'rbsafecircuit'
And then execute:
bundle install
Or install it yourself as:
gem install rbsafecircuit
Example Usage
require 'rbsafecircuit'
max_failures = 3
timeout = 10
pause_time = 5
max_consecutive_successes = 2
cb = CircuitBreaker.new(max_failures, timeout, pause_time, max_consecutive_successes)
test_function = lambda do
if rand < 0.8
puts "Function succeeded!"
"Success"
else
puts "Function failed!"
raise StandardError.new("Function failed")
end
end
cb.execute(&test_function)
cb.on_success { |result| puts "Success: #{result}" }
cb.on_failure { |error| puts "Error: #{error.message}" }
cb.set_on_open(lambda { puts "Circuit breaker is open!" })
cb.set_on_close(lambda { puts "Circuit breaker is closed." })
cb.set_on_half_open(lambda { puts "Circuit breaker is half-open." })
sleep(15)
License
The gem is available as open source under the terms of the MIT License.