Heroku Rails - 关闭Active Record Postgres连接

时间:2022-09-22 17:59:58

I have a rails 4 app I am hosting on heroku. They give some specific advice about how to manage your DB connection pool when using a multi-threaded server (puma)https://devcenter.heroku.com/articles/concurrency-and-database-connections

我有一个我在heroku上托管的rails 4应用程序。他们提供了一些有关如何在使用多线程服务器(puma)时管理数据库连接池的具体建议https://devcenter.heroku.com/articles/concurrency-and-database-connections

When I ran load testing for my app I got an error- can't connect to the db. when each page was being hit, rails initializes active record, even if I'm not making any queries on that page, or referencing any models.

当我为我的应用程序运行负载测试时出现错误 - 无法连接到数据库。当每个页面被点击时,rails会初始化活动记录,即使我没有在该页面上进行任何查询,也不会引用任何模型。

My question is:

我的问题是:

How can I make a sort of whitelist (or blacklist) so that active record is not initialized with a db connection for these specific controller actions? In an initializer?

如何制作一种白名单(或黑名单),以便不为这些特定控制器操作使用数据库连接初始化活动记录?在初始化程序中?

Ideally I would run the cheaper postgres service on heroku (40 connections) because I know my app doesnt use the db very often. If traffic hits higher that the 40 connections things will start to error, which seems silly for an app that wasn't going to use the db on those requests.

理想情况下,我会在heroku(40个连接)上运行更便宜的postgres服务,因为我知道我的应用程序不经常使用数据库。如果流量达到更高,40个连接将开始出错,这对于那些不会在这些请求上使用db的应用程序来说似乎很愚蠢。

I read about how to disable active record for an entire app: Disable ActiveRecord for Rails 4

我读到了如何禁用整个应用程序的活动记录:禁用ActiveRecord for Rails 4

But how do I selectively enable it? Are there any other different performance considerations here (by not eager loading these things or any other gotchas)

但是我该如何有选择地启用它呢?这里是否有任何其他不同的性能考虑因素(不要急于加载这些东西或任何其他陷阱)

1 个解决方案

#1


0  

In you application_controller.rb

在你的application_controller.rb中

before_filter :maybe_disconnect_db

def maybe_disconnect_db
  ActiveRecord::Base.remove_connection() if ActiveRecord::Base.connected?
end

def maybe_connect_db
  ActiveRecord::Base.establish_connection() unless ActiveRecord::Base.connected?
end

Then for each controller/action that needs the db connection add

然后为每个需要数据库连接添加的控制器/操作

skip_before_filter :maybe_disconnect_db, #only or unless filter here
before_filter      :maybe_connect_db,    #only or unless filter here

This should establish the connection for any specific db request, and disconnect for any request that doesn't need it, while also handling multiple db requests in a row without action, and multiple non db requests in a row without action.

这应该为任何特定的db请求建立连接,并断开任何不需要它的请求,同时还在没有操作的情况下连续处理多个db请求,并且在没有操作的情况下连续处理多个非db请求。

ActiveRecord::Base.remove_connection

ActiveRecord::Base.establish_connection

#1


0  

In you application_controller.rb

在你的application_controller.rb中

before_filter :maybe_disconnect_db

def maybe_disconnect_db
  ActiveRecord::Base.remove_connection() if ActiveRecord::Base.connected?
end

def maybe_connect_db
  ActiveRecord::Base.establish_connection() unless ActiveRecord::Base.connected?
end

Then for each controller/action that needs the db connection add

然后为每个需要数据库连接添加的控制器/操作

skip_before_filter :maybe_disconnect_db, #only or unless filter here
before_filter      :maybe_connect_db,    #only or unless filter here

This should establish the connection for any specific db request, and disconnect for any request that doesn't need it, while also handling multiple db requests in a row without action, and multiple non db requests in a row without action.

这应该为任何特定的db请求建立连接,并断开任何不需要它的请求,同时还在没有操作的情况下连续处理多个db请求,并且在没有操作的情况下连续处理多个非db请求。

ActiveRecord::Base.remove_connection

ActiveRecord::Base.establish_connection