module Sidekiq::CircuitBreaker::API

Public Instance Methods

circuit_open?(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 56
def circuit_open?(scope)
  Sidekiq.redis do |conn|
    conn.exists(open_key(scope))
  end
end
close_circuit(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 18
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
failure_count_for_scope(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 62
def failure_count_for_scope(scope)
  Sidekiq.redis do |conn|
    conn.get(failure_key(scope))
  end.to_i
end
open_circuit(scope, ttl = 120) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 8
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
register_failure_for_scope(scope, ttl = 120) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 29
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
register_success_for_scope(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 43
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
time_to_close_the_circuit(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 50
def time_to_close_the_circuit(scope)
  Sidekiq.redis do |conn|
    conn.ttl(open_key(scope))
  end
end

Private Instance Methods

failure_key(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 78
def failure_key(scope)
  redis_key("failure:#{scope}")
end
open_key(scope) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 74
def open_key(scope)
  redis_key("open:#{scope}")
end
redis_key(suffix) click to toggle source
# File lib/sidekiq/circuit_breaker/api.rb, line 70
def redis_key(suffix)
  "sidekiq:circuit_breaker:#{suffix}"
end