mperham / connection_pool

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

Support for recycling connections / setting a maximum age on connections #91

Closed Roguelazer closed 7 years ago

Roguelazer commented 7 years ago

It would be nice to be able to set a maximum lifetime for a connection and have it automatically destroyed on checkin once it exceeds that lifetime, so that we can forcibly recycle connections. The most pressing use-case for this is when the resource being connected to is behind a layer 4 (tcp) load balancer and you want to ensure that connections will pick up new hosts periodically. Most other connection pooling libraries that I've used have this functionality, and it seems self-evidently valuable to me.

This would require some fairly invasive changes (e.g., maintaining metadata about each connection at creation time so that we know how old it is), so I figured I'd run the concept by you before writing up a patch.

mperham commented 7 years ago

The readme says connections must be self-healing and I have no plans to change that.

Roguelazer commented 7 years ago

I'm not asking for healing support, I'm asking for pre-emptive closing support, which is not provided by any of the libraries that you cite as "self-healing" in the readme. If you're really uninterested in such functionality, I'll just fork, but I figured I'd ask first.

mperham commented 7 years ago

I don't know of anyone else asking for that functionality so I think forking is the right thing to do here.

On Nov 16, 2016, at 10:31, James Brown notifications@github.com wrote:

I'm not asking for healing support, I'm asking for pre-emptive closing support, which is not provided by any of the libraries that you cite as "self-healing" in the readme. If you're really uninterested in such functionality, I'll just fork, but I figured I'd ask first.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ce07c3 commented 7 years ago

@Roguelazer would this aid in recycling keep-alive connections? I have a host I need to connect to quickly, so ideally the connections don't live for longer than a few seconds and are then recycled unless used in the meantime. Saves roughly 200 milliseconds per request compared to re-doing the SSL handshake.

daveroberts commented 7 years ago

@mperham I'm running into this issue as well.

    def connection
      return Mysql2::Client.new(options)
    end

    def pool
      @db ||= ConnectionPool.new(size: 5, timeout: 5){ connection }
    end

    def get_users
      pool.with do |db|
        return db.query("SELECT * from users")
      end
    end

the db.query line is throwing the exception. How can I tell connection_pool "this connection is dead, throw it away and grab another one" with the existing code?

mperham commented 7 years ago

@daveroberts You cannot. From the readme:

There is no provision for repairing or checking the health of a connection; connections should be self-repairing.