Skip to content

Commit

Permalink
Fix opening and closing Redis connections instead of using a pool (#1…
Browse files Browse the repository at this point in the history
…8171)

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

* Fix Redis connections not being returned to the pool in CLI commands
  • Loading branch information
Gargron committed Apr 29, 2022
1 parent 6476f7e commit 7b0fe4a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 12 deletions.
7 changes: 6 additions & 1 deletion app/lib/redis_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

class RedisConfiguration
class << self
def establish_pool(new_pool_size)
@pool&.shutdown(&:close)
@pool = ConnectionPool.new(size: new_pool_size) { new.connection }
end

def with
pool.with { |redis| yield redis }
end

def pool
@pool ||= ConnectionPool.new(size: pool_size) { new.connection }
@pool ||= establish_pool(pool_size)
end

def pool_size
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/redisable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module Redisable
private

def redis
Thread.current[:redis] ||= RedisConfiguration.new.connection
Thread.current[:redis] ||= RedisConfiguration.pool.checkout
end
end
6 changes: 4 additions & 2 deletions config/initializers/stoplight.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'stoplight'

Stoplight::Light.default_data_store = Stoplight::DataStore::Redis.new(RedisConfiguration.new.connection)
Stoplight::Light.default_notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)]
Rails.application.reloader.to_prepare do
Stoplight::Light.default_data_store = Stoplight::DataStore::Redis.new(RedisConfiguration.new.connection)
Stoplight::Light.default_notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)]
end
12 changes: 9 additions & 3 deletions lib/mastodon/cli_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ def create_progress_bar(total = nil)
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])
Expand All @@ -52,6 +55,9 @@ def parallelize_with_progress(scope)

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/mastodon/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def clean_up_sockets!
end

def clean_up_redis_socket!
Thread.current[:redis]&.close
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end

Expand Down
7 changes: 4 additions & 3 deletions lib/mastodon/search_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def deploy
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)
Expand Down Expand Up @@ -139,6 +137,9 @@ def deploy
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
Expand Down
2 changes: 1 addition & 1 deletion lib/mastodon/sidekiq_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def clean_up_sockets!
end

def clean_up_redis_socket!
Thread.current[:redis]&.close
RedisConfiguration.pool.checkin if Thread.current[:redis]
Thread.current[:redis] = nil
end

Expand Down
2 changes: 2 additions & 0 deletions spec/services/resolve_account_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@
return_values << described_class.new.call('foo@ap.example.com')
rescue ActiveRecord::RecordNotUnique
fail_occurred = true
ensure
RedisConfiguration.pool.checkin if Thread.current[:redis]
end
end
end
Expand Down

0 comments on commit 7b0fe4a

Please sign in to comment.