tpei
tpei

Reputation: 681

How to release a Sequel connection when using a pool

I am facing some pooling problems using the Sequel gem and Postgres. I believe that my Sidekiq jobs aren't releasing the connections they use from the pool.

I connect to the DB when starting Sidekiq using:

DB = Sequel.connect(db_uri, max_connections: 20)

I was thinking of some Sidekiq middleware, like this:

module Middleware
  class SequelDisconnector
    def call(*args)
      yield
    ensure
      DB.release_active_pool_connection
    end
  end
end

I know there is DB.disconnect but that disconnects entirely from the DB right? I want to make sure my worker releases its connection while not killing any of the other workers' connections.

Is there any way to achieve this or do I have some kind of flaw in my reasoning?

Upvotes: 2

Views: 1382

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

First, you need to understand that your worker is not necessarily using a single connection, but could be using many different connections, unless you are specifically doing something to limit it to a single connection via Database#synchronize or Database#transaction.

You should not need to disconnect specific connections as it is expected that connections will remain in Sequel's connection pool for later use. Is there a reason you think the connections should be disconnected and removed from the pool?

Upvotes: 2

Related Questions