Sidekiq::CircuitBreaker

CircuitBreaker is a Sidekiq plugin that adds circuit breaker functionality.

Based on Circuit Breaker pattern.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-circuit-breaker', require: 'sidekiq/circuit_breaker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-circuit-breaker

Usage

Let's say you have a Worker called WebhookNotificationJob, that basically sends webhook notifications to extenal customer endpoints.

class WebhookNotificationJob
  include Sidekiq::Worker

  def perform(endpoint, payload)
    post_to_endpoint(endpoint, body: payload)
  end
end  

The problem of this is when some endpoint start to respond with errors or take to long to respond, the overall queue latency increases significantly.

With this gem you can apply CircuiBreaker pattern like this:

class WebhookNotificationJob
  include Sidekiq::Worker
  include Sidekiq::CircuitBreaker::Worker

  sidekiq_circuit_breaker do |config|
    config.failure_threshold = 10
    config.max_open_time     = 120
    config.set_scope ->(endpoint, _payload) do
      uri = URI.parse(endpoint)
      uri.hostname #=> The scope will be only the hostname i.e: google.com
    end
  end

  def perform(endpoint, payload)
    post_to_endpoint(endpoint, payload)
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ResultadosDigitais/sidekiq-circuitbreaker.

License

The gem is available as open source under the terms of the MIT License.