无法在5秒内获得数据库连接 - Sidekiq工作人员,Unicorn,Redis ToGo on Heroku

时间:2015-01-13 13:04:31

标签: postgresql ruby-on-rails-4 heroku sidekiq redistogo

我有一堆更新数据库的后台任务(Sidekiq工作人员),我不断收到这个失败的线程异常。

Heroku Log: WARN: could not obtain a database connection within 5.000 seconds (waited 5.001 seconds)
  • 的Heroku
  • Heroku Postgres :: Olive - 20个连接限制。
  • Redis ToGo - 10个连接限制。
  • Sidekiq - 2个连接。

每个客户端请求创建~50个线程 - 最后~20个线程试图更新db。 现在我知道这是太多的线程试图建立连接(更新Active:Record ..)。 我不介意他们再试一次,直到成功。

-config / unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 30
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  if defined?(ActiveRecord::Base)
      ActiveRecord::Base.connection.disconnect!
  end
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  if defined?(ActiveRecord::Base)
      config = ActiveRecord::Base.configurations[Rails.env] ||
          Rails.application.config.database_configuration[Rails.env]
      config['pool']            =   ENV['DB_POOL'] || 2
      config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
      ActiveRecord::Base.establish_connection(config)
  end
end

-config /初始化/ sidekiq.rb

require 'sidekiq'
  Sidekiq.configure_server do |config|
    if(database_url = ENV['DATABASE_URL'])
        p pool_size = Sidekiq.options[:concurrency] + 2
        p ENV['DATABASE_URL'] = "#{database_url}?pool=#{pool_size}"
        ActiveRecord::Base.establish_connection
    end
end

- condig / sidekiq.yml

:concurrency: 2

非常感谢所有的帮助, 埃尔达尔

0 个答案:

没有答案