0
0
Fork 0

Fix opening and closing Redis connections instead of using a pool (#18171)

* Fix opening and closing Redis connections instead of using a pool

* Fix Redis connections not being returned to the pool in CLI commands
This commit is contained in:
Eugen Rochko 2022-04-29 22:43:07 +02:00 committed by GitHub
parent 6476f7e4da
commit 7b0fe4aef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 12 deletions

View file

@ -19,15 +19,18 @@ module Mastodon
ProgressBar.create(total: total, format: '%c/%u |%b%i| %e')
end
def reset_connection_pools!
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env].dup.tap { |config| config['pool'] = options[:concurrency] + 1 })
RedisConfiguration.establish_pool(options[:concurrency])
end
def parallelize_with_progress(scope)
if options[:concurrency] < 1
say('Cannot run with this concurrency setting, must be at least 1', :red)
exit(1)
end
db_config = ActiveRecord::Base.configurations[Rails.env].dup
db_config['pool'] = options[:concurrency] + 1
ActiveRecord::Base.establish_connection(db_config)
reset_connection_pools!
progress = create_progress_bar(scope.count)
pool = Concurrent::FixedThreadPool.new(options[:concurrency])
@ -52,6 +55,9 @@ module Mastodon
result = ActiveRecord::Base.connection_pool.with_connection do
yield(item)
ensure
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end
aggregate.increment(result) if result.is_a?(Integer)

View file

@ -19,7 +19,7 @@ class Mastodon::RackMiddleware
end
def clean_up_redis_socket!
Thread.current[:redis]&.close
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end

View file

@ -59,9 +59,7 @@ module Mastodon
index.specification.lock!
end
db_config = ActiveRecord::Base.configurations[Rails.env].dup
db_config['pool'] = options[:concurrency] + 1
ActiveRecord::Base.establish_connection(db_config)
reset_connection_pools!
pool = Concurrent::FixedThreadPool.new(options[:concurrency])
added = Concurrent::AtomicFixnum.new(0)
@ -139,6 +137,9 @@ module Mastodon
sleep 1
rescue => e
progress.log pastel.red("Error importing #{index}: #{e}")
ensure
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end
end
end

View file

@ -26,7 +26,7 @@ class Mastodon::SidekiqMiddleware
end
def clean_up_redis_socket!
Thread.current[:redis]&.close
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end