mperham / connection_pool

Generic connection pooling for Ruby
MIT License
1.63k stars 143 forks source link

Use with Redis and Sinatra #115

Closed pitosalas closed 5 years ago

pitosalas commented 5 years ago

I have set my pool size to 10 but I still allocating 20 connections. 20 connects is the limit placed on me by the redis server. Here's my code. I would expect that the pool would ever only open 10 connections. But then what would it do if it was asked for an 11th? Or am I totally misunderstanding it?

def self.initialize
    return unless @pool.nil?
    @pool = ConnectionPool.new(size: 10, timeout: 5) do
      Redis.new(
        reconnect_attempts: 10,
        reconnect_delay: 1.5,
        reconnect_delay_max: 10.0
      )
    end
  end

  def self.get(key)
    @pool.with do |redis|
      redis.get(key)
    end
  end
mperham commented 5 years ago

10 connections per process. Are you forking multiple processes?

pitosalas commented 5 years ago

I am calling this from within a sinatra app, running under puma, responding to a barrage of GET calls from different (loader.io) clients. On the other hand those are all class methods and a class instance variable. So, I am not 100% sure whether I am getting another instance of the class for each one, I suspect base on your question that I am... Do you know?

mperham commented 5 years ago

If you are running in puma, you likely are starting a cluster, which forks multiple processes. You are seeing expected behavior. https://github.com/puma/puma/#clustered-mode