2024-09-02 10:19:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Mastodon::RedisConfiguration
|
2024-09-04 10:10:45 -04:00
|
|
|
DEFAULTS = {
|
|
|
|
host: 'localhost',
|
|
|
|
port: 6379,
|
|
|
|
db: 0,
|
|
|
|
}.freeze
|
|
|
|
|
2024-09-02 10:19:55 -04:00
|
|
|
def base
|
2024-09-04 10:10:45 -04:00
|
|
|
@base ||= setup_config(prefix: nil, defaults: DEFAULTS)
|
|
|
|
.merge(namespace: base_namespace)
|
2024-09-02 10:19:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def sidekiq
|
2024-09-04 10:10:45 -04:00
|
|
|
@sidekiq ||= setup_config(prefix: 'SIDEKIQ_')
|
|
|
|
.merge(namespace: sidekiq_namespace)
|
2024-09-02 10:19:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache
|
2024-09-04 10:10:45 -04: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 10:19:55 -04: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 10:10:45 -04:00
|
|
|
def setup_config(prefix: nil, defaults: {})
|
|
|
|
prefix = "#{prefix}REDIS_"
|
2024-09-02 10:19:55 -04:00
|
|
|
|
2024-09-04 10:10:45 -04: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])
|
|
|
|
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
|
|
|
|
sentinels = parse_sentinels(ENV.fetch("#{prefix}SENTINELS", nil))
|
2024-09-02 10:19:55 -04:00
|
|
|
|
2024-09-04 10:10:45 -04:00
|
|
|
return { url:, driver: } if url
|
2024-09-02 10:19:55 -04:00
|
|
|
|
2024-09-04 10:10:45 -04:00
|
|
|
if name.present? && sentinels.present?
|
|
|
|
host = name
|
|
|
|
port = nil
|
|
|
|
db ||= 0
|
|
|
|
else
|
|
|
|
sentinels = nil
|
|
|
|
end
|
2024-09-02 10:19:55 -04:00
|
|
|
|
2024-09-04 10:10:45 -04:00
|
|
|
url = construct_uri(host, port, db, user, password)
|
2024-09-02 10:19:55 -04:00
|
|
|
|
2024-09-04 10:10:45 -04:00
|
|
|
if url.present?
|
|
|
|
{ url:, driver:, name:, sentinels: }
|
2024-09-02 10:19:55 -04:00
|
|
|
else
|
2024-09-04 10:10:45 -04:00
|
|
|
# Fall back to base config. This has defaults for the URL
|
|
|
|
# so this cannot lead to an endless loop.
|
|
|
|
base
|
2024-09-02 10:19:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def construct_uri(host, port, db, user, password)
|
2024-09-04 10:10:45 -04:00
|
|
|
return nil if host.blank?
|
|
|
|
|
2024-09-02 10:19:55 -04: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 10:10:45 -04:00
|
|
|
|
|
|
|
def parse_sentinels(sentinels_string)
|
|
|
|
(sentinels_string || '').split(',').map do |sentinel|
|
|
|
|
host, port = sentinel.split(':')
|
|
|
|
port = port.present? ? port.to_i : 26_379
|
|
|
|
{ host: host, port: port }
|
|
|
|
end.presence
|
|
|
|
end
|
2024-09-02 10:19:55 -04:00
|
|
|
end
|