Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add relative_require for other timers #130

Merged
merged 5 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/object_usage_benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen-string-literal: true
# frozen_string_literal: true

require 'circuitbox'
require 'circuitbox/memory_store'
Expand Down
18 changes: 5 additions & 13 deletions lib/circuitbox/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -135,7 +131,7 @@ def half_open_failure

def open!
@state_change_mutex.synchronize do
return if open_flag?
return if open?

trip
end
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/circuitbox/configuration.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
14 changes: 7 additions & 7 deletions test/circuit_breaker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -383,23 +383,23 @@ 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)
end

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)

Expand Down