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

Don't delegate to bulkhead, it might be nil #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions lib/semian/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Semian
class CircuitBreaker #:nodoc:
extend Forwardable

attr_reader :name

def_delegators :@state, :closed?, :open?, :half_open?

def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:)
Expand Down
25 changes: 23 additions & 2 deletions lib/semian/protected_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module Semian
class ProtectedResource
extend Forwardable

def_delegators :@bulkhead, :destroy, :count, :semid, :tickets, :registered_workers, :name
def_delegators :@circuit_breaker, :reset, :mark_failed, :mark_success, :request_allowed?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we do the same if the circuit breaker is disabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i think both should just do allow_nil.

:open?, :closed?, :half_open?

attr_reader :bulkhead, :circuit_breaker
attr_reader :bulkhead, :circuit_breaker, :name

def initialize(bulkhead, circuit_breaker)
@bulkhead = bulkhead
@circuit_breaker = circuit_breaker
assign_name
end

def destroy
Expand All @@ -26,6 +26,22 @@ def acquire(timeout: nil, scope: nil, adapter: nil)
end
end

def tickets
@bulkhead ? @bulkhead.tickets : 0
end

def count
@bulkhead ? @bulkhead.count : 0
end

def registered_workers
@bulkhead ? @bulkhead.registered_workers : 0
end

def semid
@bulkhead ? @bulkhead.semid : 0
end

private

def acquire_circuit_breaker(scope, adapter)
Expand Down Expand Up @@ -54,5 +70,10 @@ def acquire_bulkhead(timeout, scope, adapter)
Semian.notify(:busy, self, scope, adapter)
raise
end

def assign_name
@name = @bulkhead.name if @bulkhead
@name ||= @circuit_breaker.name if @circuit_breaker
end
end
end
79 changes: 79 additions & 0 deletions test/protected_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,85 @@ def teardown
super
end

def test_get_name_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)

refute_nil Semian.resources[:testing].name
end

def test_get_name_without_bulkhead_or_circuit_breaker
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
circuit_breaker: false,
)

assert_nil Semian.resources[:testing].name
end

def test_get_semid_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)

assert_equal(0, Semian.resources[:testing].semid)
end

def test_get_tickets_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)

assert_equal(0, Semian.resources[:testing].tickets)
end

def test_get_count_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)

assert_equal(0, Semian.resources[:testing].count)
end

def test_get_registered_workers_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)

assert_equal(0, Semian.resources[:testing].registered_workers)
end

def test_acquire_without_bulkhead
Semian.register(
:testing,
Expand Down