Heroku:为什么有那么多数据库连接? [英] Heroku: Why so many database connections?

查看:78
本文介绍了Heroku:为什么有那么多数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Rails项目,以充当iPhone应用程序的服务器和后端数据库。在尝试进行负载测试时,我开始收到错误消息,说有太多的db:connections,当我用pg:info检查时,我发现这是真的(Connections:160/120)。

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)为什么在进程运行后这些数据库连接仍保持打开状态?

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

2)是我的配置中的某种原因导致此吗?

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)有什么办法可以

这是我第一次尝试设计可扩展的东西,如果这很简单,对不起,让我知道

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


推荐答案

在寻找了一会儿并尝试了几件事之后(减少了heroku dynos / puma worker / threads,启用了收割者 等),最终解决了我问题的是 PgBouncer

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.

不太清楚是什么引起了根本的问题,但是看起来ActiveRecord / Postgres正在打开连接以备将来使用(预期的行为)。但是,当一个新的进程开始运行并请求与数据库的连接时,AR / PG会创建一个新的连接,而不是重用旧的连接(意外行为)。我可能是错的,但是那对我来说似乎是这样,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.

这篇关于Heroku:为什么有那么多数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆