Class: Connfuse::Circuit
- Inherits:
-
Object
- Object
- Connfuse::Circuit
- Defined in:
- lib/connfuse/circuit.rb
Instance Attribute Summary collapse
-
#expected_errors ⇒ Object
readonly
Returns the value of attribute expected_errors.
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#last_failed_at ⇒ Object
readonly
Returns the value of attribute last_failed_at.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #break! ⇒ Object
- #broken? ⇒ Boolean
-
#initialize(timeout: 15, limit: 5, expected_errors: []) ⇒ Circuit
constructor
A new instance of Circuit.
- #load! ⇒ Object
- #loaded? ⇒ Boolean
- #pass_thru ⇒ Object
- #register_failure(error) ⇒ Object
Constructor Details
#initialize(timeout: 15, limit: 5, expected_errors: []) ⇒ Circuit
Returns a new instance of Circuit.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/connfuse/circuit.rb', line 13 def initialize(timeout: 15, limit: 5, expected_errors: []) @timeout = timeout @expected_errors = expected_errors @limit = limit @failure_count = 0 @status = :loaded @mutex = Mutex.new @expected_errors << CircuitError end |
Instance Attribute Details
#expected_errors ⇒ Object (readonly)
Returns the value of attribute expected_errors.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def expected_errors @expected_errors end |
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def failure_count @failure_count end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def last_error @last_error end |
#last_failed_at ⇒ Object (readonly)
Returns the value of attribute last_failed_at.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def last_failed_at @last_failed_at end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def limit @limit end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def status @status end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/connfuse/circuit.rb', line 5 def timeout @timeout end |
Instance Method Details
#break! ⇒ Object
32 33 34 35 36 |
# File 'lib/connfuse/circuit.rb', line 32 def break! @mutex.lock @status = :broken @mutex.unlock end |
#broken? ⇒ Boolean
28 29 30 |
# File 'lib/connfuse/circuit.rb', line 28 def broken? :broken == status end |
#load! ⇒ Object
38 39 40 41 42 43 |
# File 'lib/connfuse/circuit.rb', line 38 def load! @mutex.lock @status = :loaded @failure_count = 0 @mutex.unlock end |
#loaded? ⇒ Boolean
24 25 26 |
# File 'lib/connfuse/circuit.rb', line 24 def loaded? :loaded == status end |
#pass_thru ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/connfuse/circuit.rb', line 54 def pass_thru raise CircuitError if broken? && too_soon? yield rescue => e register_failure(e) break! if failure_count > limit raise e end |
#register_failure(error) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/connfuse/circuit.rb', line 45 def register_failure(error) return if expected_errors.include? error @mutex.lock @failure_count += 1 @last_error = error @last_failed_at = Time.now @mutex.unlock end |