Skip to content

Commit

Permalink
Allow to disable bulkheads in a given thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kirs committed Oct 30, 2024
1 parent c7920cc commit 8610008
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Change: semians will trigger on ER_PROXYSQL_MAX_CONN_TIMEOUT (#520)
* Change: added support for dynamic semian_configurations to the Semian Redis adapter
* Change: `Semian.disable_bulkheads_for_thread` disables bulkheads for the given thread

# v0.21.3

Expand Down
17 changes: 16 additions & 1 deletion lib/semian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ def thread_safe=(thread_safe)
@thread_safe = thread_safe
end

THREAD_BULKHEAD_DISABLED_VAR = :semian_bulkheads_disabled
private_constant(:THREAD_BULKHEAD_DISABLED_VAR)

def bulkheads_disabled_in_thread?(thread)
thread.thread_variable_get(THREAD_BULKHEAD_DISABLED_VAR)
end

def disable_bulkheads_for_thread(thread)
old_value = thread.thread_variable_get(THREAD_BULKHEAD_DISABLED_VAR)
thread.thread_variable_set(THREAD_BULKHEAD_DISABLED_VAR, true)
yield
ensure
thread.thread_variable_set(THREAD_BULKHEAD_DISABLED_VAR, old_value)
end

private

def create_circuit_breaker(name, **options)
Expand Down Expand Up @@ -318,7 +333,7 @@ def implementation(**options)
end

def create_bulkhead(name, **options)
return if ENV.key?("SEMIAN_BULKHEAD_DISABLED")
return if ENV.key?("SEMIAN_BULKHEAD_DISABLED") || bulkheads_disabled_in_thread?(Thread.current)
return unless options.fetch(:bulkhead, true)

permissions = options[:permissions] || default_permissions
Expand Down
15 changes: 15 additions & 0 deletions test/semian_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ def test_disabled_bulkhead_via_env_with_option_enabled
ENV.delete("SEMIAN_BULKHEAD_DISABLED")
end

def test_disabled_bulkhead_via_thread
Semian.disable_bulkheads_for_thread(Thread.current) do
resource = Semian.register(
:disabled_bulkhead_via_env,
bulkhead: true,
tickets: 1,
success_threshold: 1,
error_threshold: 1,
error_timeout: 1,
)

assert_nil(resource.bulkhead)
end
end

def test_disabled_circuit_breaker
resource = Semian.register(
:disabled_circuit_breaker,
Expand Down

0 comments on commit 8610008

Please sign in to comment.