随后的独角兽请求真的很慢

时间:2014-01-16 15:47:39

标签: ruby-on-rails ruby amazon-web-services unicorn

我在独角兽上运行一个rails 2应用程序并遇到奇怪的行为。当我第一次启动服务器并发出第一个请求时,它非常快(整个页面加载时<3秒)。每个后续请求需要30-45秒。当我拖尾日志时,前30秒内实际上没有记录任何内容。然后突然页面加载并且日志显示出来。好像请求只挂了30多秒。

当我使用script/serverthin运行相同的代码时,它可以快速按预期运行。

目前我们在Heroku上运行独角兽而没有这些问题。我正在迁移到AWS,而这正是我遇到问题的地方。我在运行独角兽时也在本地虚拟机上遇到同样的问题。

这是我的配置。

bundle exec unicorn -c ./config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
timeout 90
preload_app true
listen (ENV['PORT'] || 8000), :backlog => Integer(ENV['UNICORN_BACKLOG'] || 20)

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  # AR
  ActiveRecord::Base.connection.disconnect!

  # Redis
  begin
    Timeout.timeout(2) { REDIS.quit }
  rescue Exception
    # This likely means the redis service is down
  end
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  # AR
  ActiveRecord::Base.establish_connection

  # Redis
  uri = URI.parse(ENV["REDIS_URL"])
  REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

  # Dalli
  Rails.cache.reset

  # Mongoid
  Mongoid::Setup.run
end

更新

我把它缩小到妨碍工人完成的事情。我有30秒的超时,即使请求只需要1秒完成,它将不再接受任何更多的请求,直到超时发生。有没有什么好方法可以弄清楚什么阻碍了工人的可用?

0 个答案:

没有答案