Sha256: 796aa1a203af29a9359a81a7494ddd06248ee38548868087cdbdea12cbc071e1

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'connection_pool'
require 'redis'

module ApiWarden
  class RedisConnection
    class << self

      def create(options = {})
        options[:url] ||= determine_redis_provider
        size = options[:size] || 5
        pool_timeout = options[:pool_timeout] || 1
        ConnectionPool.new(:timeout => pool_timeout, :size => size) do
          build_client(options)
        end
      end

      private

      def build_client(options)
        namespace = options[:namespace]

        client = Redis.new client_opts(options)
        if namespace
          begin
            require 'redis/namespace'
            Redis::Namespace.new(namespace, :redis => client)
          rescue LoadError
            puts "Your Redis configuration uses the namespace '#{namespace}' but the redis-namespace gem is not included in the Gemfile." \
                                 "Add the gem to your Gemfile to continue using a namespace. Otherwise, remove the namespace parameter."
            exit(-127)
          end
        else
          client
        end
      end

      def client_opts(options)
        opts = options.dup
        if opts[:namespace]
          opts.delete(:namespace)
        end

        if opts[:network_timeout]
          opts[:timeout] = opts[:network_timeout]
          opts.delete(:network_timeout)
        end

        opts[:driver] ||= 'ruby'

        # redis-rb will silently retry an operation.
        # This can lead to duplicate jobs if Sidekiq::Client's LPUSH
        # is performed twice but I believe this is much, much rarer
        # than the reconnect silently fixing a problem; we keep it
        # on by default.
        opts[:reconnect_attempts] ||= 1

        opts
      end

      def determine_redis_provider
        ENV[ENV['REDIS_PROVIDER'] || 'REDIS_URL']
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
api_warden-0.3.0 lib/api_warden/redis_connection.rb
api_warden-0.2.0 lib/api_warden/redis_connection.rb
api_warden-0.1.0 lib/api_warden/redis_connection.rb