From f3dea597e4893d6e4d1e3ad3f1e37927d99c173c Mon Sep 17 00:00:00 2001 From: Ritikesh Date: Thu, 28 Mar 2019 19:22:51 +0530 Subject: [PATCH 1/4] add relative_require for other timers --- lib/circuitbox/configuration.rb | 2 ++ 1 file changed, 2 insertions(+) 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' From 61a486b73d99de3ea7385d83596d2e4dd9502cff Mon Sep 17 00:00:00 2001 From: Ritikesh Date: Fri, 29 Mar 2019 15:48:03 +0530 Subject: [PATCH 2/4] remove redundant methods in favor of aliasing --- lib/circuitbox/circuit_breaker.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/circuitbox/circuit_breaker.rb b/lib/circuitbox/circuit_breaker.rb index 1ca3cce..a62c2b9 100644 --- a/lib/circuitbox/circuit_breaker.rb +++ b/lib/circuitbox/circuit_breaker.rb @@ -78,12 +78,10 @@ def run(circuitbox_exceptions: true) end def open? - if open_flag? - true - else - false - end + circuit_store.key?(open_storage_key) end + alias :open_flag? :open? + private :open_flag? def error_rate(failures = failure_count, success = success_count) all_count = failures + success @@ -169,10 +167,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 From a6437ad3a18e8398674fef3011f6895b89039638 Mon Sep 17 00:00:00 2001 From: Ritikesh Date: Tue, 28 May 2019 01:45:58 +0530 Subject: [PATCH 3/4] replace open_flag? with open? globally --- lib/circuitbox/circuit_breaker.rb | 10 ++++------ test/circuit_breaker_test.rb | 14 +++++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/circuitbox/circuit_breaker.rb b/lib/circuitbox/circuit_breaker.rb index a62c2b9..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 @@ -80,8 +80,6 @@ def run(circuitbox_exceptions: true) def open? circuit_store.key?(open_storage_key) end - alias :open_flag? :open? - private :open_flag? def error_rate(failures = failure_count, success = success_count) all_count = failures + success @@ -121,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 @@ -133,7 +131,7 @@ def half_open_failure def open! @state_change_mutex.synchronize do - return if open_flag? + return if open? trip end @@ -158,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 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) From ded29717460ef203ea12733ad666a6fd45fec2d4 Mon Sep 17 00:00:00 2001 From: Ritikesh Date: Tue, 28 May 2019 01:51:21 +0530 Subject: [PATCH 4/4] update frozen_string_literal --- benchmark/object_usage_benchmark.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'