Sha256: 519ce9c7d6b2cdf010d9af730a51d3ab706d5617760b5c4459e344181ec81535

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'sidekiq'

module Sidekiq
  module CircuitBreaker
    module API
      extend self

      def open_circuit(scope, ttl = 120)
        raise ArgumentError, 'scope must not be nil' if scope.nil?
        Sidekiq.redis do |conn|
          conn.setex(open_key(scope), ttl, 0)
          # reset failure count
          conn.del(failure_key(scope))
        end
        true
      end

      def close_circuit(scope)
        raise ArgumentError, 'scope must not be nil' if scope.nil?
        Sidekiq.redis do |conn|
          conn.del(open_key(scope))
          # reset failure count
          conn.del(failure_key(scope))
        end
        true

      end

      def register_failure_for_scope(scope, ttl = 120)
        raise ArgumentError, 'scope must not be nil' if scope.nil?
        key = failure_key(scope)

        _, failure_count = Sidekiq.redis do |conn|
          conn.multi do
            conn.expire(key, ttl)
            conn.incr(key)
          end
        end

        failure_count
      end

      def register_success_for_scope(scope)
        raise ArgumentError, 'scope must not be nil' if scope.nil?
        Sidekiq.redis do |conn|
          conn.del(failure_key(scope), open_key(scope))
        end
      end

      def time_to_close_the_circuit(scope)
        Sidekiq.redis do |conn|
          conn.ttl(open_key(scope))
        end
      end

      def circuit_open?(scope)
        Sidekiq.redis do |conn|
          conn.exists(open_key(scope))
        end
      end

      def failure_count_for_scope(scope)
        Sidekiq.redis do |conn|
          conn.get(failure_key(scope))
        end.to_i
      end

      private

      def redis_key(suffix)
        "sidekiq:circuit_breaker:#{suffix}"
      end

      def open_key(scope)
        redis_key("open:#{scope}")
      end

      def failure_key(scope)
        redis_key("failure:#{scope}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-circuit-breaker-0.1.5 lib/sidekiq/circuit_breaker/api.rb