socketry / async-postgres

78 stars 11 forks source link

Review connection pool implementation #8

Open ioquatix opened 5 years ago

ioquatix commented 5 years ago

Regarding https://github.com/rails/rails/pull/37070 I realised maybe it's possible for long running queries to overlap because the ConnectionPool is storing connections seemingly per-thread. So even if you start a transaction in different fibers, maybe it breaks.

      # Retrieve the connection associated with the current thread, or call
      # #checkout to obtain one if necessary.
      #
      # #connection can be called any number of times; the connection is
      # held in a cache keyed by a thread.
      def connection
        @thread_cached_conns[connection_cache_key(current_thread)] ||= checkout
      end

One option is to try and hijack the ConnectionPool - e.g. if spec[:connection_pool] is defined, use it.

        message_bus.instrument("!connection.active_record", payload) do
          owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
        end

It seems to me, a per-fiber or per-thread connection pool might make more sense.