Heroku: Why so many database connections?

此生再无相见时 提交于 2019-12-11 13:25:38

问题


I'm working on a rails project to act as a server and backend database for an iPhone app. In my attempts to do load testing I started getting errors saying there were too many db:connections and when I checked with pg:info I saw that to be true (Connections: 160/120).

This made some sense to me during the test but after it's over it still shows that many open connections which really confuses me. My questions

1) Why are these db connections remaining open after the process is done running?

2) Is it something in my configuration that's causing this?

rails: 4.2.4
Postgres: 9.4.4
Heroku dynos: five 1x-dynos
Web Server: Puma
Puma Workers: 2
Puma Threads: 9
Database Pool: ENV["DB_POOL"] || ENV['MAX_THREADS'] || 6 (so probably also 9 as MAX_THREADS is set to that)

3) Is there some way I can set the connections to close after the process is finished running?

This is my first time trying to design something to scale so sorry if this is basic as hell and let me know if there's anything I forgot to include.

puma.rb

 workers Integer(ENV['WEB_CONCURRENCY'] || 2)
 threads_count = Integer(ENV['MAX_THREADS'] || 6)
 threads threads_count, threads_count

 preload_app!

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

 on_worker_boot do
   # Worker specific setup for Rails 4.1+
   # See: https://devcenter.heroku.com/articles/deploying-rail-applications-with-the-puma-web-server#on-worker-boot
   ActiveRecord::Base.establish_connection
 end

回答1:


After looking for awhile and trying several things (decreasing heroku dynos/puma workers/threads, enabling the rails 'reaper', etc.), what ended up fixing my issue was PgBouncer.

Not too sure what was causing the underlying issue, but it looked like ActiveRecord/Postgres was leaving connections open for future use (expected behavior). However when a new process began running and requested a connection to the database, AR/PG would create a new connection instead of reusing the old (unexpected behavior). I could be wrong but that's how it appeared to me and PgBouncer seems to have taken care of that. Hopefully this helps anyone else with the same problem.



来源:https://stackoverflow.com/questions/35301152/heroku-why-so-many-database-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!