Sha256: ddc0cf2e90da63581affd9876239b214dcddc34933330ddabb30ecc1218d3ea8

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'aasm'

#
# CircuitState is created individually for each object, and keeps
# track of how the object is doing and whether the object's circuit
# has tripped or not.
#
class CircuitBreaker::CircuitState

  include AASM

  aasm_state :half_open

  aasm_state :open

  aasm_state :closed, :enter => :reset_failure_count

  aasm_initial_state :closed

  #
  # Trips the circuit breaker into the open state where it will immediately fail.
  #
  aasm_event :trip do
    transitions :to => :open, :from => [:closed, :half_open]
  end

  #
  # Transitions from an open state to a half_open state.
  #
  aasm_event :attempt_reset do
    transitions :to => :half_open, :from => [:open]
  end

  #
  # Close the circuit from an open or half open state.
  #
  aasm_event :reset do
    transitions :to => :closed, :from => [:open, :half_open]
  end

  def initialize()
    @failure_count = 0
    @last_failure_time = nil
  end

  attr_accessor :last_failure_time

  attr_accessor :failure_count

  def increment_failure_count
    @failure_count = @failure_count + 1
    @last_failure_time = Time.now
  end

  def reset_failure_count
    @failure_count = 0
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
circuit_breaker-1.1.0 lib/circuit_breaker/circuit_state.rb
circuit_breaker-1.0.1 lib/circuit_breaker/circuit_state.rb