From b2797ba5af6a40450a1bf9fe05e6587a58c7a6d6 Mon Sep 17 00:00:00 2001 From: Dale Hamel Date: Mon, 3 Apr 2017 12:02:30 -0400 Subject: [PATCH 1/2] Must be able to access name without a bulkhead --- lib/semian/circuit_breaker.rb | 2 ++ lib/semian/protected_resource.rb | 10 ++++++++-- test/protected_resource_test.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/semian/circuit_breaker.rb b/lib/semian/circuit_breaker.rb index ad19f3fa..f93af871 100644 --- a/lib/semian/circuit_breaker.rb +++ b/lib/semian/circuit_breaker.rb @@ -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:) diff --git a/lib/semian/protected_resource.rb b/lib/semian/protected_resource.rb index 6ff3c4a0..d097547d 100644 --- a/lib/semian/protected_resource.rb +++ b/lib/semian/protected_resource.rb @@ -2,15 +2,16 @@ module Semian class ProtectedResource extend Forwardable - def_delegators :@bulkhead, :destroy, :count, :semid, :tickets, :registered_workers, :name + def_delegators :@bulkhead, :destroy, :count, :semid, :tickets, :registered_workers def_delegators :@circuit_breaker, :reset, :mark_failed, :mark_success, :request_allowed?, :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 @@ -54,5 +55,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 diff --git a/test/protected_resource_test.rb b/test/protected_resource_test.rb index 2f50b87d..1d905a81 100644 --- a/test/protected_resource_test.rb +++ b/test/protected_resource_test.rb @@ -16,6 +16,33 @@ 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_acquire_without_bulkhead Semian.register( :testing, From 6cc3bae004531a54b369604582cee43207a88020 Mon Sep 17 00:00:00 2001 From: Dale Hamel Date: Mon, 3 Apr 2017 12:26:56 -0400 Subject: [PATCH 2/2] Don't delegate anything to bulkhead --- lib/semian/protected_resource.rb | 17 ++++++++++- test/protected_resource_test.rb | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/semian/protected_resource.rb b/lib/semian/protected_resource.rb index d097547d..87575858 100644 --- a/lib/semian/protected_resource.rb +++ b/lib/semian/protected_resource.rb @@ -2,7 +2,6 @@ module Semian class ProtectedResource extend Forwardable - def_delegators :@bulkhead, :destroy, :count, :semid, :tickets, :registered_workers def_delegators :@circuit_breaker, :reset, :mark_failed, :mark_success, :request_allowed?, :open?, :closed?, :half_open? @@ -27,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) diff --git a/test/protected_resource_test.rb b/test/protected_resource_test.rb index 1d905a81..3c10653c 100644 --- a/test/protected_resource_test.rb +++ b/test/protected_resource_test.rb @@ -43,6 +43,58 @@ def test_get_name_without_bulkhead_or_circuit_breaker 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,