gpt4 book ai didi

ruby-on-rails - ActiveRecord::ConnectionTimeoutError 与 Heroku

转载 作者:行者123 更新时间:2023-11-29 12:25:42 25 4
gpt4 key购买 nike

我在 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 个测功机,大小为 Performance L,并发性为 15
  • 对于 Web 服务器,我使用 1 dyno 和 2 puma worker 和 15 线程,所以总数是 (1 * 2 * 15 = 30)。
  • 我的数据库池大小是 20(我使用的是 Standard-0 postgres,它最多有 120 个连接)。

那么当连接池大小大于可以使用的最大连接数时,我是如何得到这个错误的呢?总共 (15 + 40 = 45) 从 120

这是我的配置文件:

- condig/sidekiq.yml

    concurrency: 15
:queues:
- myapp

- 配置/inititalizers/sidekiq.rb

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
}

- 配置/puma

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

- database.yml

production:
pool: 20

最佳答案

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

引用:https://devcenter.heroku.com/articles/concurrency-and-database-connections

关于ruby-on-rails - ActiveRecord::ConnectionTimeoutError 与 Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39043105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com