2022-04-29 01:47:34 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-09-03 00:19:55 +10:00
|
|
|
class RedisConnection
|
2022-04-29 01:47:34 +10:00
|
|
|
class << self
|
2022-04-30 06:43:07 +10:00
|
|
|
def establish_pool(new_pool_size)
|
|
|
|
@pool&.shutdown(&:close)
|
|
|
|
@pool = ConnectionPool.new(size: new_pool_size) { new.connection }
|
|
|
|
end
|
|
|
|
|
2022-09-27 11:08:19 +10:00
|
|
|
delegate :with, to: :pool
|
2022-04-29 01:47:34 +10:00
|
|
|
|
|
|
|
def pool
|
2022-04-30 06:43:07 +10:00
|
|
|
@pool ||= establish_pool(pool_size)
|
2022-04-29 01:47:34 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def pool_size
|
|
|
|
if Sidekiq.server?
|
2022-09-27 11:08:19 +10:00
|
|
|
Sidekiq[:concurrency]
|
2022-04-29 01:47:34 +10:00
|
|
|
else
|
|
|
|
ENV['MAX_THREADS'] || 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-03 00:19:55 +10:00
|
|
|
attr_reader :config
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@config = REDIS_CONFIGURATION.base
|
|
|
|
end
|
|
|
|
|
2022-04-29 01:47:34 +10:00
|
|
|
def connection
|
2024-09-03 00:19:55 +10:00
|
|
|
namespace = config[:namespace]
|
|
|
|
if namespace.present?
|
2022-04-29 01:47:34 +10:00
|
|
|
Redis::Namespace.new(namespace, redis: raw_connection)
|
|
|
|
else
|
|
|
|
raw_connection
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def raw_connection
|
2024-09-03 00:19:55 +10:00
|
|
|
Redis.new(**config)
|
2022-04-29 01:47:34 +10:00
|
|
|
end
|
|
|
|
end
|