diff --git a/benchmark/object_usage_benchmark.rb b/benchmark/object_usage_benchmark.rb index 8c1a7d0..3b64735 100644 --- a/benchmark/object_usage_benchmark.rb +++ b/benchmark/object_usage_benchmark.rb @@ -1,4 +1,4 @@ -# frozen-string-literal: true +# frozen_string_literal: true require 'circuitbox' require 'circuitbox/memory_store' diff --git a/lib/circuitbox/circuit_breaker.rb b/lib/circuitbox/circuit_breaker.rb index 1ca3cce..e12aa03 100644 --- a/lib/circuitbox/circuit_breaker.rb +++ b/lib/circuitbox/circuit_breaker.rb @@ -53,7 +53,7 @@ def option_value(name) end def run(circuitbox_exceptions: true) - if open_flag? + if open? skipped! raise Circuitbox::OpenCircuitError.new(service) if circuitbox_exceptions else @@ -78,11 +78,7 @@ def run(circuitbox_exceptions: true) end def open? - if open_flag? - true - else - false - end + circuit_store.key?(open_storage_key) end def error_rate(failures = failure_count, success = success_count) @@ -123,7 +119,7 @@ def passed_rate_threshold?(rate) def half_open_failure @state_change_mutex.synchronize do - return if open_flag? || !half_open? + return if open? || !half_open? trip end @@ -135,7 +131,7 @@ def half_open_failure def open! @state_change_mutex.synchronize do - return if open_flag? + return if open? trip end @@ -160,7 +156,7 @@ def close! # If the circuit is not open, the half_open key will be deleted from the store # if half_open exists the deleted value is returned and allows us to continue # if half_open doesn't exist nil is returned, causing us to return early - return unless !open_flag? && circuit_store.delete(half_open_storage_key) + return unless !open? && circuit_store.delete(half_open_storage_key) end # Running event outside of the synchronize block to allow other threads @@ -169,10 +165,6 @@ def close! logger.debug(circuit_closed_message) end - def open_flag? - circuit_store.key?(open_storage_key) - end - def half_open? circuit_store.key?(half_open_storage_key) end diff --git a/lib/circuitbox/configuration.rb b/lib/circuitbox/configuration.rb index c4cedbd..9235c47 100644 --- a/lib/circuitbox/configuration.rb +++ b/lib/circuitbox/configuration.rb @@ -1,4 +1,6 @@ require_relative 'memory_store' +require_relative 'timer/monotonic' +require_relative 'timer/null' require_relative 'timer/simple' require_relative 'notifier/active_support' require_relative 'notifier/null' diff --git a/test/circuit_breaker_test.rb b/test/circuit_breaker_test.rb index ec13328..a71223a 100644 --- a/test/circuit_breaker_test.rb +++ b/test/circuit_breaker_test.rb @@ -154,7 +154,7 @@ def setup end def test_raises_when_circuit_is_open - @circuit.stubs(open_flag?: true) + @circuit.stubs(open?: true) assert_raises(Circuitbox::OpenCircuitError) { @circuit.run {} } end @@ -304,7 +304,7 @@ def test_success_does_not_clear_half_open_when_circuit_is_open # since we're testing a threading issue without running multiple threads # we need the circuit to run the first time - circuit.stubs(:open_flag?).returns(false, true) + circuit.stubs(:open?).returns(false, true) circuit.run { circuit_ran = true } @@ -353,7 +353,7 @@ def test_records_response_failure def test_records_response_skipped circuit = Circuitbox::CircuitBreaker.new(:yammer, exceptions: [Timeout::Error]) - circuit.stubs(open_flag?: true) + circuit.stubs(open?: true) circuit.stubs(:notify_event) circuit.expects(:notify_event).with('skipped') emulate_circuit_run(circuit, :failure, Timeout::Error) @@ -383,9 +383,9 @@ def test_puts_circuit_to_sleep_once_opened circuit = Circuitbox::CircuitBreaker.new(:yammer, exceptions: [Timeout::Error]) circuit.stubs(should_open?: true) - assert !circuit.send(:open_flag?) + assert !circuit.send(:open?) emulate_circuit_run(circuit, :failure, Timeout::Error) - assert circuit.send(:open_flag?) + assert circuit.send(:open?) circuit.expects(:open!).never emulate_circuit_run(circuit, :failure, Timeout::Error) @@ -393,13 +393,13 @@ def test_puts_circuit_to_sleep_once_opened def test_open_is_true_if_open_flag circuit = Circuitbox::CircuitBreaker.new(:yammer, exceptions: [Timeout::Error]) - circuit.stubs(open_flag?: true) + circuit.stubs(open?: true) assert circuit.open? end def test_open_is_false_if_awake_and_under_rate_threshold circuit = Circuitbox::CircuitBreaker.new(:yammer, exceptions: [Timeout::Error]) - circuit.stubs(open_flag?: false, + circuit.stubs(open?: false, passed_volume_threshold?: false, passed_rate_threshold: false)