class Disyuntor
Constants
- CircuitOpenError
- VERSION
Attributes
failures[R]
opened_at[R]
state[R]
threshold[R]
timeout[R]
Public Class Methods
new(threshold:, timeout:)
click to toggle source
# File lib/disyuntor.rb, line 6 def initialize(threshold:, timeout:) @threshold = threshold @timeout = timeout on_circuit_open { fail CircuitOpenError } close! end
Public Instance Methods
closed?()
click to toggle source
# File lib/disyuntor.rb, line 28 def closed? state == :closed end
on_circuit_open(&block)
click to toggle source
# File lib/disyuntor.rb, line 23 def on_circuit_open(&block) raise ArgumentError, "Must pass a block" unless block_given? @on_circuit_open = block end
open?()
click to toggle source
# File lib/disyuntor.rb, line 32 def open? not (closed? or timed_out?) end
try(&block)
click to toggle source
# File lib/disyuntor.rb, line 15 def try(&block) if closed? or timed_out? circuit_closed(&block) else circuit_open end end
Private Instance Methods
circuit_closed(&block)
click to toggle source
# File lib/disyuntor.rb, line 63 def circuit_closed(&block) ret = block.call rescue open! if increment_failures! >= threshold raise else close! ret end
circuit_open()
click to toggle source
# File lib/disyuntor.rb, line 73 def circuit_open @on_circuit_open.call(self) end
close!()
click to toggle source
# File lib/disyuntor.rb, line 40 def close! @opened_at = nil @failures = 0 @state = :closed end
increment_failures!()
click to toggle source
# File lib/disyuntor.rb, line 59 def increment_failures! @failures += 1 end
next_timeout_at()
click to toggle source
# File lib/disyuntor.rb, line 55 def next_timeout_at opened_at + timeout end
open!()
click to toggle source
# File lib/disyuntor.rb, line 46 def open! @opened_at = Time.now.to_i @state = :open end
timed_out?()
click to toggle source
# File lib/disyuntor.rb, line 51 def timed_out? Time.now.to_i > next_timeout_at end