Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord::与Heroku的ConnectionTimeoutError_Ruby On Rails_Postgresql_Activerecord_Heroku_Sidekiq - Fatal编程技术网

Ruby on rails ActiveRecord::与Heroku的ConnectionTimeoutError

Ruby on rails ActiveRecord::与Heroku的ConnectionTimeoutError,ruby-on-rails,postgresql,activerecord,heroku,sidekiq,Ruby On Rails,Postgresql,Activerecord,Heroku,Sidekiq,我在Heroku中使用Sidekiq反复出现了一个ActiveRecord::ConnectionTimeoutError。以下是全部错误: ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.008 seconds); all pooled connections were in use 我在Sidekiq中使用1 d

我在Heroku中使用Sidekiq反复出现了一个
ActiveRecord::ConnectionTimeoutError
。以下是全部错误:

ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.008 seconds); all pooled connections were in use
  • 我在Sidekiq中使用1 dyno,大小
    Performance L
    ,并发性
    15
  • 对于web服务器,我使用了
    1
    dyno,每个
    2
    puma workers和
    15
    线程,因此总数为(1*2*15=30)
  • 我的数据库池大小是
    20
    (我使用的是
    Standard-0
    postgres,最多有120个连接)
那么,当连接池大小大于可以使用的最大连接数时,我是如何得到这个错误的呢?总计(15+40=45)从120

以下是我的配置文件:

-
condig/sidekiq.yml
-
condig/inititalizers/sidekiq.rb
-
config/puma
-
database.yml

本例中的问题是puma vs数据库配置,您的puma服务器将尝试生成30个线程,但为所有线程共享的数据库池的配置仅设置为20个连接(它忽略了您的数据库仅使用配置中的内容即可处理120个连接),因此10个puma线程将不断遇到该错误

Ref:

这显示了活动记录如何在
work()
方法完成后正确释放连接。它描述了运动鞋的问题,但也可以用于Sidekiq,因为这是活动记录问题

def work(msg)
  id = JSON.parse(msg)['id']
  ActiveRecord::Base.connection_pool.with_connection do
    user = User.find(id)
    user.name="Homer Simpson"
    user.save
  end
  ack!
end
Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add Sidekiq::Status::ClientMiddleware
  end
end

Sidekiq.configure_server do |config|
  config.redis = { url: ENV["REDISTOGO_URL"] } if ENV["REDISTOGO_URL"].present?

  config.server_middleware do |chain|
    chain.add Sidekiq::Status::ServerMiddleware, expiration: 30.minutes
  end
  config.client_middleware do |chain|
    chain.add Sidekiq::Status::ClientMiddleware
  end
end

Sidekiq.configure_client do |config|
  config.redis = { url: ENV["REDISTOGO_URL"] } if ENV["REDISTOGO_URL"].present?
end

if Rails.env.production?
  Sidekiq.configure_server do |config|
    config.redis = { url: ENV["REDISTOGO_URL"] }
  end

  Sidekiq.configure_client do |config|
    config.redis = { url: ENV["REDISTOGO_URL"] }
  end
end

Sidekiq.default_worker_options = {
  queue: "myapp",
  backtrace: true,
  retry: false
}
workers Integer(ENV["WEB_CONCURRENCY"] || 2)
threads_count = Integer(ENV["RAILS_MAX_THREADS"] || 15)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV["PORT"]     || 3000
environment ENV["RACK_ENV"] || "development"

on_worker_boot do
  ActiveRecord::Base.establish_connection
end
production:
  pool: 20
def work(msg)
  id = JSON.parse(msg)['id']
  ActiveRecord::Base.connection_pool.with_connection do
    user = User.find(id)
    user.name="Homer Simpson"
    user.save
  end
  ack!
end