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

fix: Provide thread names whenever possible #316

Merged
merged 5 commits into from
Mar 10, 2025
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 launchdarkly-server-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency "semantic", "~> 1.6"
spec.add_runtime_dependency "concurrent-ruby", "~> 1.1"
spec.add_runtime_dependency "ld-eventsource", "2.2.2"
spec.add_runtime_dependency "ld-eventsource", "2.2.3"
spec.add_runtime_dependency "observer", "~> 0.1.2"
spec.add_runtime_dependency "zlib", "~> 3.1" unless RUBY_PLATFORM == "java"
# Please keep ld-eventsource dependency as an exact version so that bugfixes to
Expand Down
6 changes: 3 additions & 3 deletions lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,17 @@ def initialize(inbox, sdk_key, config, diagnostic_accumulator, event_sender)
@events_in_last_batch = 0

outbox = EventBuffer.new(config.capacity, config.logger)
flush_workers = NonBlockingThreadPool.new(MAX_FLUSH_WORKERS)
flush_workers = NonBlockingThreadPool.new(MAX_FLUSH_WORKERS, 'LD/EventDispatcher/FlushWorkers')

if !@diagnostic_accumulator.nil?
diagnostic_event_workers = NonBlockingThreadPool.new(1)
diagnostic_event_workers = NonBlockingThreadPool.new(1, 'LD/EventDispatcher/DiagnosticEventWorkers')
init_event = @diagnostic_accumulator.create_init_event(config)
send_diagnostic_event(init_event, diagnostic_event_workers)
else
diagnostic_event_workers = nil
end

Thread.new { main_loop(inbox, outbox, flush_workers, diagnostic_event_workers) }
Thread.new { main_loop(inbox, outbox, flush_workers, diagnostic_event_workers) }.name = "LD/EventDispatcher#main_loop"
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/impl/big_segments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(big_segments_config, logger)

unless @store.nil?
@cache = ExpiringCache.new(big_segments_config.context_cache_size, big_segments_config.context_cache_time)
@poll_worker = RepeatingTask.new(big_segments_config.status_poll_interval, 0, -> { poll_store_and_update_status }, logger)
@poll_worker = RepeatingTask.new(big_segments_config.status_poll_interval, 0, -> { poll_store_and_update_status }, logger, 'LD/BigSegments#status')
@poll_worker.start
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/ldclient-rb/impl/integrations/file_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def initialize(resolved_paths, interval, reloader, logger)
end
end
end
@thread.name = "LD/FileDataSource"
end

def stop
Expand Down
3 changes: 3 additions & 0 deletions lib/ldclient-rb/impl/migrations/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ def write(key, context, default_stage, payload = nil)
auth_handler = Thread.new { authoritative_result = authoritative.run }
nonauth_handler = Thread.new { nonauthoritative_result = nonauthoritative.run }

auth_handler.name = "LD/Migrator#auth_handler"
nonauth_handler.name = "LD/Migrator#nonauth_handler"

auth_handler.join()
nonauth_handler.join()
when LaunchDarkly::Migrations::MigratorBuilder::EXECUTION_RANDOM && @sampler.sample(2)
Expand Down
7 changes: 6 additions & 1 deletion lib/ldclient-rb/impl/repeating_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
module LaunchDarkly
module Impl
class RepeatingTask
def initialize(interval, start_delay, task, logger)
attr_reader :name

def initialize(interval, start_delay, task, logger, name)
@interval = interval
@start_delay = start_delay
@task = task
@logger = logger
@stopped = Concurrent::AtomicBoolean.new(false)
@worker = nil
@name = name
end

def start
Expand All @@ -31,6 +34,8 @@ def start
end
end
end

@worker.name = @name
end

def stop
Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/impl/store_client_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def monitoring_enabled?

@logger.warn("Detected persistent store unavailability; updates will be cached until it recovers.")

task = Impl::RepeatingTask.new(0.5, 0, -> { self.check_availability }, @logger)
task = Impl::RepeatingTask.new(0.5, 0, -> { self.check_availability }, @logger, 'LD/StoreWrapper#check_availability')

@mutex.synchronize do
@poller = task
Expand Down
4 changes: 2 additions & 2 deletions lib/ldclient-rb/non_blocking_thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module LaunchDarkly
# than blocking. Also provides a way to wait for all jobs to finish without shutting down.
# @private
class NonBlockingThreadPool
def initialize(capacity)
def initialize(capacity, name = 'LD/NonBlockingThreadPool')
@capacity = capacity
@pool = Concurrent::FixedThreadPool.new(capacity)
@pool = Concurrent::FixedThreadPool.new(capacity, name: name)
@semaphore = Concurrent::Semaphore.new(capacity)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/polling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(config, requestor)
@initialized = Concurrent::AtomicBoolean.new(false)
@started = Concurrent::AtomicBoolean.new(false)
@ready = Concurrent::Event.new
@task = Impl::RepeatingTask.new(@config.poll_interval, 0, -> { self.poll }, @config.logger)
@task = Impl::RepeatingTask.new(@config.poll_interval, 0, -> { self.poll }, @config.logger, 'LD/PollingDataSource')
end

def initialized?
Expand Down
14 changes: 11 additions & 3 deletions spec/impl/repeating_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ def null_logger
double.as_null_object
end

it "can name the task" do
signal = Concurrent::Event.new
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "Junie B.")

expect(task.name).to eq("Junie B.")
task.stop
end

it "does not start when created" do
signal = Concurrent::Event.new
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger)
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "test")
begin
expect(signal.wait(0.1)).to be false
ensure
Expand All @@ -23,7 +31,7 @@ def null_logger

it "executes until stopped" do
queue = Queue.new
task = RepeatingTask.new(0.1, 0, -> { queue << Time.now }, null_logger)
task = RepeatingTask.new(0.1, 0, -> { queue << Time.now }, null_logger, "test")
begin
last = nil
task.start
Expand Down Expand Up @@ -62,7 +70,7 @@ def null_logger
stopped.set
end
},
null_logger)
null_logger, "test")
begin
task.start
expect(stopped.wait(0.1)).to be true
Expand Down