mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: avoid spinning a thread each time we close a connection
This is a temporary workaround for the issue in https://github.com/rails/rails/pull/36949 Discussing a proper fix in Rails with the Rails team. Prior to this fix we were spinning up a thread every time we closed a connection to the db.
This commit is contained in:
56
script/thread_detective.rb
Normal file
56
script/thread_detective.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Thread
|
||||
attr_accessor :origin
|
||||
end
|
||||
|
||||
class ThreadDetective
|
||||
def self.test_thread
|
||||
Thread.new { sleep 1 }
|
||||
end
|
||||
def self.start(max_threads)
|
||||
@thread ||= Thread.new do
|
||||
self.new.monitor(max_threads)
|
||||
end
|
||||
|
||||
@trace = TracePoint.new(:thread_begin) do |tp|
|
||||
Thread.current.origin = Thread.current.inspect
|
||||
end
|
||||
@trace.enable
|
||||
end
|
||||
|
||||
def self.stop
|
||||
@thread&.kill
|
||||
@thread = nil
|
||||
@trace&.disable
|
||||
@trace.stop
|
||||
end
|
||||
|
||||
def monitor(max_threads)
|
||||
STDERR.puts "Monitoring threads in #{Process.pid}"
|
||||
|
||||
while true
|
||||
threads = Thread.list
|
||||
|
||||
if threads.length > max_threads
|
||||
str = +("-" * 60)
|
||||
str << "#{threads.length} found in Process #{Process.pid}!\n"
|
||||
|
||||
threads.each do |thread|
|
||||
str << "\n"
|
||||
if thread.origin
|
||||
str << thread.origin
|
||||
else
|
||||
str << thread.inspect
|
||||
end
|
||||
str << "\n"
|
||||
end
|
||||
str << ("-" * 60)
|
||||
|
||||
STDERR.puts str
|
||||
end
|
||||
sleep 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user