正确设置单线程Rails应用的数据库连接池database.yml [英] Correct setting of database connection pool database.yml for single-threaded Rails applications

查看:15
本文介绍了正确设置单线程Rails应用的数据库连接池database.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 Rails database.yml 中的以下设置:

默认情况下,ActiveRecord的连接池的数据库连接数设置为5:

开发:...池:5

但默认情况下,Rails 3 是单线程的.为什么默认情况下需要 5 个连接?

据我所知,一个单线程的 Rails 应用不能同时触发多个数据库操作,为什么需要保持更多的连接打开?

我认为 2 个连接是有意义的,所以即使另一个连接超时,您也始终有一个活动连接,但保持五个连接对我来说似乎有点奇怪.

我错过了什么吗?

更新如果其他人很好奇,我刚刚找到了一个解释它的提交:https://github.com/rails/rails/commit/b700153507b7d539a57a57a6e57a6e

事实上,这些默认设置没有任何意义,它已被修复,但由于测试套件而暂时恢复(一年前).

解决方案

来这里参加聚会已经很晚了,但我今天在生产中用完了数据库连接.

像很多人一样,我使用 Sidekiq 来执行异步工作,例如发送电子邮件.需要注意的是,Sidekiq 作为多线程进程运行.

所以,我没有只是有一个单线程 Rails 应用程序,因此这个答案并不直接适用于所提出的问题,但我认为值得在这里说点什么,因为我认为多线程 Rails现在的应用程序相对正常.

这意味着您需要以这样一种方式调整池大小,以创建足够的连接来处理所有可以入队且耗时超过 5 秒的作业(在抛出请求之前等待数据库连接的默认超时时间)错误).

I was wondering about the following setting in the Rails database.yml:

By default the number of database connections for the connection pool of ActiveRecord is set to 5:

development:
  ...
  pool: 5

But by default, Rails 3 is single threaded. Why would you need 5 connections by default?

As far as I understand, a single threaded Rails app can't trigger multiple database operations at once, why would do you need to keep more connection open?

I would assume that 2 connections would make sense, so you always have one active connection even if the other one times out, but holding five connections seems a little odd to me.

Am I missing something?

UPDATE If anyone else is curious, I just found a commit that explains it: https://github.com/rails/rails/commit/b700153507b7d539a57a6e3bcf03c84776795051

In fact these default settings don't make any sense, it was fixed but then temporarily reverted (one year ago) because of the test suite.

解决方案

Quite late to the party here, but I ran out of database connections today in production.

Like a lot of people, I use Sidekiq to perform asynchronous jobs like sending emails for example. It is important to note that Sidekiq runs as a multithread process.

So, I don't just have a single-threaded Rails application, therefore this answer does not directly apply to the question asked but I thought it was worth saying something here as I think multithreaded Rails apps are relatively normal nowadays.

This means you need to adjust your pool size in such a way as to create enough connections to handle all the jobs that can be enqueued and take longer than 5 seconds (the default timeout period to wait for a database connection before throwing an error).

这篇关于正确设置单线程Rails应用的数据库连接池database.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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