Class: SharedBroker::CircuitBreaker

Inherits:
Object
  • Object
show all
Defined in:
lib/shared_broker/circuit_breaker.rb

Defined Under Namespace

Classes: OpenError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(failure_threshold: 5, recovery_timeout: 30) ⇒ CircuitBreaker

Returns a new instance of CircuitBreaker.



11
12
13
14
15
16
17
18
# File 'lib/shared_broker/circuit_breaker.rb', line 11

def initialize(failure_threshold: 5, recovery_timeout: 30)
  @failure_threshold = failure_threshold
  @recovery_timeout = recovery_timeout
  @state = :closed
  @failure_count = 0
  @last_failure_time = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#failure_countObject (readonly)

Returns the value of attribute failure_count.



9
10
11
# File 'lib/shared_broker/circuit_breaker.rb', line 9

def failure_count
  @failure_count
end

#failure_thresholdObject (readonly)

Returns the value of attribute failure_threshold.



9
10
11
# File 'lib/shared_broker/circuit_breaker.rb', line 9

def failure_threshold
  @failure_threshold
end

#recovery_timeoutObject (readonly)

Returns the value of attribute recovery_timeout.



9
10
11
# File 'lib/shared_broker/circuit_breaker.rb', line 9

def recovery_timeout
  @recovery_timeout
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/shared_broker/circuit_breaker.rb', line 9

def state
  @state
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shared_broker/circuit_breaker.rb', line 20

def run
  check_state!

  begin
    result = yield
    success!
    result
  rescue => e
    record_failure!
    raise e
  end
end