class Cachext::Breaker::WithKey

Attributes

config[R]
key[R]

Public Class Methods

new(config, key) click to toggle source
# File lib/cachext/breaker.rb, line 18
def initialize(config, key)
  @config = config
  @key = key
end

Public Instance Methods

check_health() click to toggle source
# File lib/cachext/breaker.rb, line 30
def check_health
  if half_open?
    if health_check >= config.failure_threshold
      reset!
    else
      increment_health_check
    end
  end
end
half_open?() click to toggle source
# File lib/cachext/breaker.rb, line 50
def half_open?
  state == :half_open
end
health_check() click to toggle source
# File lib/cachext/breaker.rb, line 73
def health_check
  lock_redis.get(key_str(:health_check)).to_i
end
increment_failure() click to toggle source
# File lib/cachext/breaker.rb, line 23
def increment_failure
  lock_redis.pipelined do
    lock_redis.set key_str(:last_failure), Time.now.to_f
    lock_redis.incr key_str(:monitor)
  end
end
increment_health_check() click to toggle source
# File lib/cachext/breaker.rb, line 77
def increment_health_check
  lock_redis.incr key_str(:health_check)
end
key_str(name) click to toggle source
# File lib/cachext/breaker.rb, line 81
def key_str(name)
  "cachext:#{name}:#{key.raw.map(&:to_s).join(":")}"
end
last_failure() click to toggle source
# File lib/cachext/breaker.rb, line 68
def last_failure
  lf = lock_redis.get key_str(:last_failure)
  lf.nil? ? nil : lf.to_f
end
lock_redis() click to toggle source
# File lib/cachext/breaker.rb, line 85
def lock_redis
  config.lock_redis
end
monitor() click to toggle source
# File lib/cachext/breaker.rb, line 64
def monitor
  lock_redis.get(key_str(:monitor)).to_i
end
open?() click to toggle source
# File lib/cachext/breaker.rb, line 46
def open?
  state == :open
end
reset!() click to toggle source
# File lib/cachext/breaker.rb, line 40
def reset!
  lock_redis.del key_str(:monitor),
            key_str(:health_check),
            key_str(:last_failure)
end
state() click to toggle source
# File lib/cachext/breaker.rb, line 54
def state
  if (lf = last_failure) && (lf + config.breaker_timeout < Time.now.to_f)
    :half_open
  elsif monitor >= config.failure_threshold
    :open
  else
    :close
  end
end