2024-09-02 15:19:55 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Mastodon::RedisConfiguration
|
2024-09-04 15:10:45 +01:00
|
|
|
DEFAULTS = {
|
|
|
|
host: 'localhost',
|
|
|
|
port: 6379,
|
|
|
|
db: 0,
|
|
|
|
}.freeze
|
|
|
|
|
2024-09-02 15:19:55 +01:00
|
|
|
def base
|
2024-09-04 15:10:45 +01:00
|
|
|
@base ||= setup_config(prefix: nil, defaults: DEFAULTS)
|
|
|
|
.merge(namespace: base_namespace)
|
2024-09-02 15:19:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def sidekiq
|
2024-09-04 15:10:45 +01:00
|
|
|
@sidekiq ||= setup_config(prefix: 'SIDEKIQ_')
|
|
|
|
.merge(namespace: sidekiq_namespace)
|
2024-09-02 15:19:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache
|
2024-09-04 15:10:45 +01:00
|
|
|
@cache ||= setup_config(prefix: 'CACHE_')
|
|
|
|
.merge({
|
|
|
|
namespace: cache_namespace,
|
|
|
|
expires_in: 10.minutes,
|
|
|
|
connect_timeout: 5,
|
|
|
|
pool: {
|
|
|
|
size: Sidekiq.server? ? Sidekiq[:concurrency] : Integer(ENV['MAX_THREADS'] || 5),
|
|
|
|
timeout: 5,
|
|
|
|
},
|
|
|
|
})
|
2024-09-02 15:19:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def driver
|
|
|
|
ENV['REDIS_DRIVER'] == 'ruby' ? :ruby : :hiredis
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
|
|
|
@namespace ||= ENV.fetch('REDIS_NAMESPACE', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_namespace
|
|
|
|
return "mastodon_test#{ENV.fetch('TEST_ENV_NUMBER', nil)}" if Rails.env.test?
|
|
|
|
|
|
|
|
namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def sidekiq_namespace
|
|
|
|
namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_namespace
|
|
|
|
namespace ? "#{namespace}_cache" : 'cache'
|
|
|
|
end
|
|
|
|
|
2024-09-04 15:10:45 +01:00
|
|
|
def setup_config(prefix: nil, defaults: {})
|
|
|
|
prefix = "#{prefix}REDIS_"
|
2024-09-02 15:19:55 +01:00
|
|
|
|
2024-09-05 15:06:58 +01:00
|
|
|
url = ENV.fetch("#{prefix}URL", nil)
|
|
|
|
user = ENV.fetch("#{prefix}USER", nil)
|
|
|
|
password = ENV.fetch("#{prefix}PASSWORD", nil)
|
|
|
|
host = ENV.fetch("#{prefix}HOST", defaults[:host])
|
|
|
|
port = ENV.fetch("#{prefix}PORT", defaults[:port])
|
|
|
|
db = ENV.fetch("#{prefix}DB", defaults[:db])
|
2024-09-02 15:19:55 +01:00
|
|
|
|
2024-09-04 15:10:45 +01:00
|
|
|
return { url:, driver: } if url
|
2024-09-02 15:19:55 +01:00
|
|
|
|
2024-09-05 15:06:58 +01:00
|
|
|
sentinel_options = setup_sentinels(prefix, default_user: user, default_password: password)
|
2024-09-05 10:26:49 +01:00
|
|
|
|
2024-09-05 15:06:58 +01:00
|
|
|
if sentinel_options.present?
|
|
|
|
host = sentinel_options[:name]
|
2024-09-04 15:10:45 +01:00
|
|
|
port = nil
|
|
|
|
db ||= 0
|
|
|
|
end
|
2024-09-02 15:19:55 +01:00
|
|
|
|
2024-09-04 15:10:45 +01:00
|
|
|
url = construct_uri(host, port, db, user, password)
|
2024-09-02 15:19:55 +01:00
|
|
|
|
2024-09-04 15:10:45 +01:00
|
|
|
if url.present?
|
2024-09-05 15:06:58 +01:00
|
|
|
{ url:, driver: }.merge(sentinel_options)
|
2024-09-02 15:19:55 +01:00
|
|
|
else
|
2024-09-05 15:06:58 +01:00
|
|
|
# Fall back to base config, which has defaults for the URL
|
|
|
|
# so this cannot lead to endless recursion.
|
2024-09-04 15:10:45 +01:00
|
|
|
base
|
2024-09-02 15:19:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-05 15:06:58 +01:00
|
|
|
def setup_sentinels(prefix, default_user: nil, default_password: nil)
|
|
|
|
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
|
|
|
|
sentinel_port = ENV.fetch("#{prefix}SENTINEL_PORT", 26_379)
|
|
|
|
sentinel_list = ENV.fetch("#{prefix}SENTINELS", nil)
|
|
|
|
sentinel_username = ENV.fetch("#{prefix}SENTINEL_USERNAME", default_user)
|
|
|
|
sentinel_password = ENV.fetch("#{prefix}SENTINEL_PASSWORD", default_password)
|
|
|
|
|
|
|
|
sentinels = parse_sentinels(sentinel_list, default_port: sentinel_port)
|
|
|
|
|
|
|
|
if name.present? && sentinels.present?
|
|
|
|
{ name:, sentinels:, sentinel_username:, sentinel_password: }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-02 15:19:55 +01:00
|
|
|
def construct_uri(host, port, db, user, password)
|
2024-09-04 15:10:45 +01:00
|
|
|
return nil if host.blank?
|
|
|
|
|
2024-09-02 15:19:55 +01:00
|
|
|
Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri|
|
|
|
|
uri.user = user if user.present?
|
|
|
|
uri.password = password if password.present?
|
|
|
|
end.normalize.to_str
|
|
|
|
end
|
2024-09-04 15:10:45 +01:00
|
|
|
|
2024-09-05 10:26:49 +01:00
|
|
|
def parse_sentinels(sentinels_string, default_port: 26_379)
|
2024-09-04 15:10:45 +01:00
|
|
|
(sentinels_string || '').split(',').map do |sentinel|
|
|
|
|
host, port = sentinel.split(':')
|
2024-09-05 10:26:49 +01:00
|
|
|
port = (port || default_port).to_i
|
2024-09-04 15:10:45 +01:00
|
|
|
{ host: host, port: port }
|
|
|
|
end.presence
|
|
|
|
end
|
2024-09-02 15:19:55 +01:00
|
|
|
end
|