mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-25 22:15:11 +01:00
7b0fe4aef9
* Fix opening and closing Redis connections instead of using a pool * Fix Redis connections not being returned to the pool in CLI commands
53 lines
869 B
Ruby
53 lines
869 B
Ruby
# frozen_string_literal: true
|
|
|
|
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 ||= establish_pool(pool_size)
|
|
end
|
|
|
|
def pool_size
|
|
if Sidekiq.server?
|
|
Sidekiq.options[:concurrency]
|
|
else
|
|
ENV['MAX_THREADS'] || 5
|
|
end
|
|
end
|
|
end
|
|
|
|
def connection
|
|
if namespace?
|
|
Redis::Namespace.new(namespace, redis: raw_connection)
|
|
else
|
|
raw_connection
|
|
end
|
|
end
|
|
|
|
def namespace?
|
|
namespace.present?
|
|
end
|
|
|
|
def namespace
|
|
ENV.fetch('REDIS_NAMESPACE', nil)
|
|
end
|
|
|
|
def url
|
|
ENV['REDIS_URL']
|
|
end
|
|
|
|
private
|
|
|
|
def raw_connection
|
|
Redis.new(url: url, driver: :hiredis)
|
|
end
|
|
end
|