独角兽!和PostgreSQL

时间:2011-11-24 21:51:14

标签: ruby-on-rails-3 postgresql unicorn

我正在将旧的Rails和PostgreSQL应用程序从2.1升级到Rails 3(通过成功的中间版2.3.11步骤)。

最后,当我让所有Rspecs顺利运行时,我安装了Mongrel 1.2.0.pre2并启动并运行 - 一切正常。即使是有100个并发用户的JMeter测试用例也没有通过。

但是当我和Unicorn一起尝试的时候!服务器,我自己使用它时工作正常,但是当我用JMeter加载它时,它开始给出100%的错误,JMeter尝试POST登录。在我查看某个特定页面的同时,它给出了我这样的奇怪错误:

undefined method `eq' for nil:NilClass

在终端,我得到以下输出(但只有几次):

message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle
WARNING:  there is already a transaction in progress

编辑:这似乎是共享PGconnect对象的问题,所以我删除了以前的一些文字

我发现PostgreSQL docs指定在发出并发请求的线程之间不能共享PGconn对象。

独角兽!或AR混合来自同一PGconn对象中不同线程的请求?

database.yml中:

production:
  adapter: postgresql
  encoding: unicode
  database: ***
  username: ***
  password: ***
  host: 127.0.0.1

我甚至试图添加这个无济于事:

  allow_concurrency: true

unicorn.conf:

worker_processes 8
working_directory "/full/path/to/app"
listen '/tmp/app.sock', :backlog => 512
timeout 30
pid "/full/path/to/app/tmp/pids/puppetmaster_unicorn.pid"

preload_app true
  if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

规格:

  • Rails 3.0.6(由于部署环境)
  • 独角兽! 4.1.1
  • Nginx 1.0.5
  • pg gem 0.11.0
  • PostgreSQL 9.0.4
  • Mac OS X 10.7.2

如果Rails版本应该是罪魁祸首,当然我可以升级到最新版本。我刚开始讲述服务提供商的所作所为。

1 个答案:

答案 0 :(得分:7)

在进程之间共享数据库连接至关重要。当您使用preload_app运行Rails时,Rails将在unicorn master分叉工作者之前建立AR连接。示例unicorn.conf.rb建议断开before_fork挂钩中的AR连接。 AR将自动重新建立新连接,因为每个工作人员都需要后叉。

摘自http://unicorn.bogomips.org/examples/unicorn.conf.rb

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
  # ...
end