0

I have a Rails 6 application with Postgres 9.6 using sneakers to receive some 10-20 messages/second from RabbitMQ.

Every now and then I get this error:

ERROR: [Exception error="could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use" error_class=ActiveRecord::ConnectionTimeoutError worker_class=...

My database configuration:

default: &default
  adapter: postgresql
  encoding: unicode
  host: postgres
  port: 5432
  pool: 150
  url: <%= ENV['DATABASE_POSTGRES_URL'] %>
  connect_timeout: 2
  checkout_timeout: 10 # error shows 5 :(

The code:

# frozen_string_literal: true
class MarketDataReceiver
    include ::Sneakers::Worker
    from_queue "#{Rails.env}.#{ENV['CLUSTER_NAME']}.market-data"

    def work(msg)
        # exit if node is central!
        return reject! if ENV['CLUSTER_NAME'].downcase.include? "central"
        market_data = ActiveSupport::JSON.decode(msg).deep_symbolize_keys
        if market_data
            if market_data[:markets]
                market_data[:markets] do |market|
                        PostgresRecord.connection_pool.with_connection do
                            @res = ::MarketBuilder.do(market, false, true)
                        end
                end
                return ack!
            end
        end
    end
end

Sneakers is configured with 1 worker and 25 threads/prefetch.

I have PG configured to up to 1000 connections, plenty of RAM and CPU space and doing a SELECT sum(numbackends) FROM pg_stat_database; shows only around 60 connections used.

Am I doing something wrong?

Nick M
  • 2,424
  • 5
  • 34
  • 57

0 Answers0