Class: CircuitB::Fuse
- Inherits:
-
Object
- Object
- CircuitB::Fuse
- Defined in:
- lib/circuit_b/fuse.rb
Constant Summary collapse
- DEFAULT_BREAK_HANDLER_TIMEOUT =
Maximum time the handler is allowed to execute
5
- STANDARD_HANDLERS =
Standard handlers that can be refered by their names
{ :rails_log => lambda { |fuse| RAILS_DEFAULT_LOGGER.error("CircuitB: Fuse '#{fuse.name}' has broken") } }
Instance Attribute Summary collapse
-
#break_handler_timeout ⇒ Object
Returns the value of attribute break_handler_timeout.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #failures ⇒ Object
-
#initialize(name, state_storage, config) ⇒ Fuse
constructor
A new instance of Fuse.
- #open? ⇒ Boolean
- #wrap(&block) ⇒ Object
Constructor Details
#initialize(name, state_storage, config) ⇒ Fuse
Returns a new instance of Fuse.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/circuit_b/fuse.rb', line 17 def initialize(name, state_storage, config) raise "Name must be specified" if name.nil? raise "Storage must be specified" if state_storage.nil? raise "Storage must be of CircuitB::Storage::Base kind" unless state_storage.kind_of?(CircuitB::Storage::Base) raise "Config must be specified" if config.nil? @name = name @state_storage = state_storage @config = config @break_handler_timeout = DEFAULT_BREAK_HANDLER_TIMEOUT end |
Instance Attribute Details
#break_handler_timeout ⇒ Object
Returns the value of attribute break_handler_timeout.
15 16 17 |
# File 'lib/circuit_b/fuse.rb', line 15 def break_handler_timeout @break_handler_timeout end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/circuit_b/fuse.rb', line 14 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/circuit_b/fuse.rb', line 14 def name @name end |
Instance Method Details
#failures ⇒ Object
59 60 61 |
# File 'lib/circuit_b/fuse.rb', line 59 def failures get(:failures) end |
#open? ⇒ Boolean
55 56 57 |
# File 'lib/circuit_b/fuse.rb', line 55 def open? get(:state) == :open end |
#wrap(&block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/circuit_b/fuse.rb', line 30 def wrap(&block) close_if_cooled_off if open? raise CircuitB::FastFailure if open? begin if @config[:timeout] && @config[:timeout].to_f > 0 Timeout::timeout(@config[:timeout].to_f) { block.call } else block.call end put(:failures, 0) rescue Exception => e # Save the time of the last failure put(:last_failure_at, Time.now.to_i) # Increment the number of failures and open if the limit has been reached failures = inc(:failures) open if failures >= @config[:allowed_failures] # Re-raise the original exception raise e end end |