Class: Breaker::Circuit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fuseObject

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

#closeObject



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?

Returns:

  • (Boolean)


72
73
74
# File 'lib/breaker.rb', line 72

def closed?
  fuse.state == :closed
end

#failure_countObject



81
82
83
# File 'lib/breaker.rb', line 81

def failure_count
  fuse.failure_count
end

#failure_thresholdObject



85
86
87
# File 'lib/breaker.rb', line 85

def failure_threshold
  fuse.failure_threshold
end

#nameObject



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?

Returns:

  • (Boolean)


67
68
69
# File 'lib/breaker.rb', line 67

def open?
  fuse.state == :open
end

#retry_timeoutObject



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

#timeoutObject



89
90
91
# File 'lib/breaker.rb', line 89

def timeout
  fuse.timeout
end