Skip to content

Commit 1000549

Browse files
committed
FIX: properly spin down unused streamer threads
Previous version was prone to the bug: ruby-concurrency/concurrent-ruby#1075 This is particularly bad cause we could have a DB connection attached to the thread and we never clear it up, so after N hours this could start exhibiting weird connection issues.
1 parent a4033e2 commit 1000549

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/ai_bot/response_http_streamer.rb

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ class ResponseHttpStreamer
88

99
class << self
1010
def thread_pool
11+
# we use our thread pool implementation here for a few reasons:
12+
#
13+
# 1. Free multisite support
14+
# 2. Unlike Concurrent::CachedThreadPool, we spin back down to 0 threads automatiaclly see: https://github.com/ruby-concurrency/concurrent-ruby/issues/1075
15+
# 3. Better internal error handling
1116
@thread_pool ||=
12-
Concurrent::CachedThreadPool.new(min_threads: 0, max_threads: POOL_SIZE, idletime: 30)
17+
Scheduler::ThreadPool.new(min_threads: 0, max_threads: POOL_SIZE, idle_time: 30)
1318
end
1419

1520
def schedule_block(&block)
16-
# think about a better way to handle cross thread connections
17-
if Rails.env.test?
18-
block.call
19-
return
20-
end
21-
22-
db = RailsMultisite::ConnectionManagement.current_db
2321
thread_pool.post do
2422
begin
25-
RailsMultisite::ConnectionManagement.with_connection(db) { block.call }
23+
block.call
2624
rescue StandardError => e
2725
Discourse.warn_exception(e, message: "Discourse AI: Unable to stream reply")
2826
end

0 commit comments

Comments
 (0)