Skip to content

Commit 6c30537

Browse files
authored
fix: Provide thread names whenever possible (#316)
1 parent 39b1e0e commit 6c30537

File tree

10 files changed

+30
-13
lines changed

10 files changed

+30
-13
lines changed

launchdarkly-server-sdk.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
3737

3838
spec.add_runtime_dependency "semantic", "~> 1.6"
3939
spec.add_runtime_dependency "concurrent-ruby", "~> 1.1"
40-
spec.add_runtime_dependency "ld-eventsource", "2.2.2"
40+
spec.add_runtime_dependency "ld-eventsource", "2.2.3"
4141
spec.add_runtime_dependency "observer", "~> 0.1.2"
4242
spec.add_runtime_dependency "zlib", "~> 3.1" unless RUBY_PLATFORM == "java"
4343
# Please keep ld-eventsource dependency as an exact version so that bugfixes to

lib/ldclient-rb/events.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ def initialize(inbox, sdk_key, config, diagnostic_accumulator, event_sender)
243243
@events_in_last_batch = 0
244244

245245
outbox = EventBuffer.new(config.capacity, config.logger)
246-
flush_workers = NonBlockingThreadPool.new(MAX_FLUSH_WORKERS)
246+
flush_workers = NonBlockingThreadPool.new(MAX_FLUSH_WORKERS, 'LD/EventDispatcher/FlushWorkers')
247247

248248
if !@diagnostic_accumulator.nil?
249-
diagnostic_event_workers = NonBlockingThreadPool.new(1)
249+
diagnostic_event_workers = NonBlockingThreadPool.new(1, 'LD/EventDispatcher/DiagnosticEventWorkers')
250250
init_event = @diagnostic_accumulator.create_init_event(config)
251251
send_diagnostic_event(init_event, diagnostic_event_workers)
252252
else
253253
diagnostic_event_workers = nil
254254
end
255255

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

259259
private

lib/ldclient-rb/impl/big_segments.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def initialize(big_segments_config, logger)
2424

2525
unless @store.nil?
2626
@cache = ExpiringCache.new(big_segments_config.context_cache_size, big_segments_config.context_cache_time)
27-
@poll_worker = RepeatingTask.new(big_segments_config.status_poll_interval, 0, -> { poll_store_and_update_status }, logger)
27+
@poll_worker = RepeatingTask.new(big_segments_config.status_poll_interval, 0, -> { poll_store_and_update_status }, logger, 'LD/BigSegments#status')
2828
@poll_worker.start
2929
end
3030
end

lib/ldclient-rb/impl/integrations/file_data_source.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def initialize(resolved_paths, interval, reloader, logger)
216216
end
217217
end
218218
end
219+
@thread.name = "LD/FileDataSource"
219220
end
220221

221222
def stop

lib/ldclient-rb/impl/migrations/migrator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def write(key, context, default_stage, payload = nil)
188188
auth_handler = Thread.new { authoritative_result = authoritative.run }
189189
nonauth_handler = Thread.new { nonauthoritative_result = nonauthoritative.run }
190190

191+
auth_handler.name = "LD/Migrator#auth_handler"
192+
nonauth_handler.name = "LD/Migrator#nonauth_handler"
193+
191194
auth_handler.join()
192195
nonauth_handler.join()
193196
when LaunchDarkly::Migrations::MigratorBuilder::EXECUTION_RANDOM && @sampler.sample(2)

lib/ldclient-rb/impl/repeating_task.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
module LaunchDarkly
66
module Impl
77
class RepeatingTask
8-
def initialize(interval, start_delay, task, logger)
8+
attr_reader :name
9+
10+
def initialize(interval, start_delay, task, logger, name)
911
@interval = interval
1012
@start_delay = start_delay
1113
@task = task
1214
@logger = logger
1315
@stopped = Concurrent::AtomicBoolean.new(false)
1416
@worker = nil
17+
@name = name
1518
end
1619

1720
def start
@@ -31,6 +34,8 @@ def start
3134
end
3235
end
3336
end
37+
38+
@worker.name = @name
3439
end
3540

3641
def stop

lib/ldclient-rb/impl/store_client_wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def monitoring_enabled?
9999

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

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

104104
@mutex.synchronize do
105105
@poller = task

lib/ldclient-rb/non_blocking_thread_pool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module LaunchDarkly
88
# than blocking. Also provides a way to wait for all jobs to finish without shutting down.
99
# @private
1010
class NonBlockingThreadPool
11-
def initialize(capacity)
11+
def initialize(capacity, name = 'LD/NonBlockingThreadPool')
1212
@capacity = capacity
13-
@pool = Concurrent::FixedThreadPool.new(capacity)
13+
@pool = Concurrent::FixedThreadPool.new(capacity, name: name)
1414
@semaphore = Concurrent::Semaphore.new(capacity)
1515
end
1616

lib/ldclient-rb/polling.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(config, requestor)
1313
@initialized = Concurrent::AtomicBoolean.new(false)
1414
@started = Concurrent::AtomicBoolean.new(false)
1515
@ready = Concurrent::Event.new
16-
@task = Impl::RepeatingTask.new(@config.poll_interval, 0, -> { self.poll }, @config.logger)
16+
@task = Impl::RepeatingTask.new(@config.poll_interval, 0, -> { self.poll }, @config.logger, 'LD/PollingDataSource')
1717
end
1818

1919
def initialized?

spec/impl/repeating_task_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ def null_logger
1111
double.as_null_object
1212
end
1313

14+
it "can name the task" do
15+
signal = Concurrent::Event.new
16+
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "Junie B.")
17+
18+
expect(task.name).to eq("Junie B.")
19+
task.stop
20+
end
21+
1422
it "does not start when created" do
1523
signal = Concurrent::Event.new
16-
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger)
24+
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "test")
1725
begin
1826
expect(signal.wait(0.1)).to be false
1927
ensure
@@ -23,7 +31,7 @@ def null_logger
2331

2432
it "executes until stopped" do
2533
queue = Queue.new
26-
task = RepeatingTask.new(0.1, 0, -> { queue << Time.now }, null_logger)
34+
task = RepeatingTask.new(0.1, 0, -> { queue << Time.now }, null_logger, "test")
2735
begin
2836
last = nil
2937
task.start
@@ -62,7 +70,7 @@ def null_logger
6270
stopped.set
6371
end
6472
},
65-
null_logger)
73+
null_logger, "test")
6674
begin
6775
task.start
6876
expect(stopped.wait(0.1)).to be true

0 commit comments

Comments
 (0)