Class: Magick::CircuitBreaker
- Inherits:
-
Object
- Object
- Magick::CircuitBreaker
- Defined in:
- lib/magick/circuit_breaker.rb
Constant Summary collapse
- DEFAULT_FAILURE_THRESHOLD =
5- DEFAULT_TIMEOUT =
60
Instance Attribute Summary collapse
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#last_failure_time ⇒ Object
readonly
Returns the value of attribute last_failure_time.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(failure_threshold: DEFAULT_FAILURE_THRESHOLD, timeout: DEFAULT_TIMEOUT) ⇒ CircuitBreaker
constructor
A new instance of CircuitBreaker.
- #open? ⇒ Boolean
Constructor Details
#initialize(failure_threshold: DEFAULT_FAILURE_THRESHOLD, timeout: DEFAULT_TIMEOUT) ⇒ CircuitBreaker
Returns a new instance of CircuitBreaker.
10 11 12 13 14 15 16 17 |
# File 'lib/magick/circuit_breaker.rb', line 10 def initialize(failure_threshold: DEFAULT_FAILURE_THRESHOLD, timeout: DEFAULT_TIMEOUT) @failure_threshold = failure_threshold @timeout = timeout @failure_count = 0 @last_failure_time = nil @state = :closed @mutex = Mutex.new end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
8 9 10 |
# File 'lib/magick/circuit_breaker.rb', line 8 def failure_count @failure_count end |
#last_failure_time ⇒ Object (readonly)
Returns the value of attribute last_failure_time.
8 9 10 |
# File 'lib/magick/circuit_breaker.rb', line 8 def last_failure_time @last_failure_time end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/magick/circuit_breaker.rb', line 8 def state @state end |
Instance Method Details
#call ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/magick/circuit_breaker.rb', line 19 def call return false if open? begin result = yield record_success result rescue StandardError => e record_failure raise e end end |
#open? ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/magick/circuit_breaker.rb', line 32 def open? @mutex.synchronize do if @state == :open if Time.now.to_i - @last_failure_time.to_i > @timeout @state = :half_open @failure_count = 0 false else true end else false end end end |