独角兽工人的第一反应慢了几秒钟

Aar*_*ter 4 ruby ruby-on-rails unicorn

我无法弄清楚如何让我的独角兽工人在他们真正"准备好"处理请求时才接受连接.我发现前几个请求很慢,然后它们会急剧加速(从几秒到几百毫秒).这个问题似乎更加复杂,因为独角兽似乎在一段时间后杀死了工人,这意味着我经常面临缓慢的第一次请求的性能损失.有没有人看过这个或者知道我能做些什么?

Aar*_*ter 6

It turns out the our i18n yml files being lazily loaded in the views on the first request was causing the performance issues. Simply adding the following to my config/unicorn.rb seems to have solved the issue:

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!

  # No need to disconnect from Redis servers--they are connected to lazily.

  # Force translations to be loaded into memory.
  I18n.t('activerecord')
end
Run Code Online (Sandbox Code Playgroud)

  • 可能是更好的代码组织在rails初始化器中执行`I18n.t('activerecord')`而不是隐藏在before_fork挂钩中.当unicorn执行应用程序预加载时,初始化程序会在fork前运行. (2认同)