redis_connection.rb 792 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. class RedisConnection
  3. class << self
  4. def establish_pool(new_pool_size)
  5. @pool&.shutdown(&:close)
  6. @pool = ConnectionPool.new(size: new_pool_size) { new.connection }
  7. end
  8. delegate :with, to: :pool
  9. def pool
  10. @pool ||= establish_pool(pool_size)
  11. end
  12. def pool_size
  13. if Sidekiq.server?
  14. Sidekiq[:concurrency]
  15. else
  16. ENV['MAX_THREADS'] || 5
  17. end
  18. end
  19. end
  20. attr_reader :config
  21. def initialize
  22. @config = REDIS_CONFIGURATION.base
  23. end
  24. def connection
  25. namespace = config[:namespace]
  26. if namespace.present?
  27. Redis::Namespace.new(namespace, redis: raw_connection)
  28. else
  29. raw_connection
  30. end
  31. end
  32. private
  33. def raw_connection
  34. Redis.new(**config)
  35. end
  36. end