Class: Connfuse::Circuit

Inherits:
Object
  • Object
show all
Defined in:
lib/connfuse/circuit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_errorsObject (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_countObject (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_errorObject (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_atObject (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

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/connfuse/circuit.rb', line 5

def limit
  @limit
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/connfuse/circuit.rb', line 5

def status
  @status
end

#timeoutObject (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

Returns:

  • (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

Returns:

  • (Boolean)


24
25
26
# File 'lib/connfuse/circuit.rb', line 24

def loaded?
  :loaded == status
end

#pass_thruObject



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