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

Allow to disable bulkheads in a given thread #562

Merged
merged 1 commit into from
Nov 4, 2024
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you expect threads other than current to be passed in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I generally don't. I've decided on that API to make it clear to the user that it disables it on a specific thread, not all threads.

I could also make it disable_bulkheads_for_current_thread but that seemed more verbose.

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
Loading