Class: Breaker::Circuit
- Inherits:
-
Object
- Object
- Breaker::Circuit
- Defined in:
- lib/breaker.rb
Instance Attribute Summary collapse
-
#fuse ⇒ Object
Returns the value of attribute fuse.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean (also: #up?)
- #failure_count ⇒ Object
- #failure_threshold ⇒ Object
-
#initialize(fuse) ⇒ Circuit
constructor
A new instance of Circuit.
- #name ⇒ Object
- #open(clock = Time.now) ⇒ Object
- #open? ⇒ Boolean (also: #down?)
- #retry_timeout ⇒ Object
- #run(clock = Time.now) ⇒ Object
- #timeout ⇒ Object
Constructor Details
#initialize(fuse) ⇒ Circuit
Returns a new instance of Circuit.
44 45 46 |
# File 'lib/breaker.rb', line 44 def initialize(fuse) @fuse = fuse end |
Instance Attribute Details
#fuse ⇒ Object
Returns the value of attribute fuse.
42 43 44 |
# File 'lib/breaker.rb', line 42 def fuse @fuse end |
Instance Method Details
#==(other) ⇒ Object
63 64 65 |
# File 'lib/breaker.rb', line 63 def ==(other) other.instance_of?(self.class) && fuse == other.fuse end |
#close ⇒ Object
57 58 59 60 61 |
# File 'lib/breaker.rb', line 57 def close fuse.failure_count = 0 fuse.state = :closed fuse.retry_threshold = nil end |
#closed? ⇒ Boolean Also known as: up?
72 73 74 |
# File 'lib/breaker.rb', line 72 def closed? fuse.state == :closed end |
#failure_count ⇒ Object
81 82 83 |
# File 'lib/breaker.rb', line 81 def failure_count fuse.failure_count end |
#failure_threshold ⇒ Object
85 86 87 |
# File 'lib/breaker.rb', line 85 def failure_threshold fuse.failure_threshold end |
#name ⇒ Object
48 49 50 |
# File 'lib/breaker.rb', line 48 def name fuse.name end |
#open(clock = Time.now) ⇒ Object
52 53 54 55 |
# File 'lib/breaker.rb', line 52 def open(clock = Time.now) fuse.state = :open fuse.retry_threshold = clock + retry_timeout end |
#open? ⇒ Boolean Also known as: down?
67 68 69 |
# File 'lib/breaker.rb', line 67 def open? fuse.state == :open end |
#retry_timeout ⇒ Object
77 78 79 |
# File 'lib/breaker.rb', line 77 def retry_timeout fuse.retry_timeout end |
#run(clock = Time.now) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/breaker.rb', line 93 def run(clock = Time.now) if closed? || half_open?(clock) begin result = Timeout.timeout timeout do yield end if half_open?(clock) close end result rescue => ex fuse.failure_count = fuse.failure_count + 1 open clock if tripped? raise ex end else raise Breaker::CircuitOpenError, "Cannot run code while #{name} is open!" end end |
#timeout ⇒ Object
89 90 91 |
# File 'lib/breaker.rb', line 89 def timeout fuse.timeout end |