Passenger Nginx如何处理并发请求

时间:2014-12-09 01:40:11

标签: mysql ruby-on-rails nginx concurrency passenger

我试图了解Passenger Nginx如何处理并发请求。我重新加载了一个页面并查看了生产日志。每次页面都会向服务器发送几个同时的ajax请求。有些任务需要昂贵的数据库查询,称为“状态”,“指导性模板”和“#39; mentorship_templates'和'指导'。通过分析日志,两次尝试似乎给了我不同的结果。

第一次尝试给了我一样的日志:

Started GET "/admin/mentorships" for 162.213.96.35 at 2014-12-09 08:13:07 +0800
Processing by Admin::MentorshipsController#index as HTML
Completed 200 OK in 351ms (Views: 190.9ms | ActiveRecord: 148.0ms)

Started GET "/admin/task_templates/?with_types=true" for 162.213.96.35 at 2014-12-09 08:13:09 +0800
Started GET "/admin/statuses/?with_stages=true" for 162.213.96.35 at 2014-12-09 08:13:09 +0800
Started GET "/admin/mentorship_task_rules/" for 162.213.96.35 at 2014-12-09 08:13:09 +0800
Started GET "/admin/mentorship_templates/" for 162.213.96.35 at 2014-12-09 08:13:09 +0800 
Started GET "/admin/substages/" for 162.213.96.35 at 2014-12-09 08:13:09 +0800

Processing by Admin::TaskTemplatesController#index as HTML
  Parameters: {"with_types"=>"true"}
Processing by Admin::MentorshipTaskRulesController#index as HTML
Processing by Admin::StatusesController#index as HTML            [expensive db query]
  Parameters: {"with_stages"=>"true"}
Processing by Admin::MentorshipTemplatesController#index as HTML [expensive db query]
Processing by Admin::SubstagesController#index as HTML

Completed 200 OK in 308ms (Views: 0.5ms | ActiveRecord: 296.2ms)
Completed 200 OK in 391ms (Views: 155.6ms | ActiveRecord: 215.7ms)
Completed 200 OK in 429ms (Views: 3.4ms | ActiveRecord: 399.6ms)

Started GET "/admin/mentorships.json" for 162.213.96.35 at 2014-12-09 08:13:10 +0800
Started GET "/major_aliases/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:13:10 +0800

Processing by MajorAliasesController#list_for_typeahead as HTML
Processing by Admin::MentorshipsController#index as JSON        [expensive db query]

Started GET "/institutes_aliases/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:13:10 +0800
Started GET "/admin/applicants/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:13:10 +0800

Processing by InstitutesAliasesController#list_for_typeahead as HTML
Processing by Admin::ApplicantsController#list_for_typeahead as HTML

Read fragment views/palmdrive.cn/institutes_aliases/list_for_typeahead (0.5ms)
Completed 200 OK in 214ms (ActiveRecord: 39.1ms)
Completed 200 OK in 370ms (Views: 14.2ms | ActiveRecord: 174.2ms)

Started GET "/admin/advisors/list_for_ajax" for 162.213.96.35 at 2014-12-09 08:13:10 +0800

Processing by Admin::AdvisorsController#list_for_ajax as HTML

Completed 200 OK in 882ms (Views: 345.8ms | ActiveRecord: 198.8ms)
Completed 200 OK in 383ms (Views: 65.9ms | ActiveRecord: 84.4ms)
Completed 200 OK in 5166ms (Views: 79.2ms | ActiveRecord: 4544.3ms)    [statuses]
Completed 200 OK in 9193ms (Views: 670.0ms | ActiveRecord: 6973.8ms)   [mentorship_templates]
Completed 200 OK in 16588ms (Views: 113.8ms | ActiveRecord: 13952.2ms) [mentorships]

在这里,我可以看到请求同时处理。昂贵的数据库查询并没有阻止其余的请求。

过了一会儿,我又试了一次,得到了日志:

Started GET "/admin/mentorships" for 162.213.96.35 at 2014-12-09 08:44:25 +0800
Processing by Admin::MentorshipsController#index as HTML
Completed 200 OK in 206ms (Views: 61.0ms | ActiveRecord: 128.7ms)

Started GET "/admin/task_templates/?with_types=true" for 162.213.96.35 at 2014-12-09 08:44:27 +0800
Processing by Admin::TaskTemplatesController#index as HTML
  Parameters: {"with_types"=>"true"}
Completed 200 OK in 263ms (Views: 59.6ms | ActiveRecord: 172.0ms)

Started GET "/admin/mentorship_task_rules/" for 162.213.96.35 at 2014-12-09 08:44:28 +0800
Processing by Admin::MentorshipTaskRulesController#index as HTML
Completed 200 OK in 169ms (Views: 0.9ms | ActiveRecord: 156.2ms)

Started GET "/admin/mentorship_templates/" for 162.213.96.35 at 2014-12-09 08:44:28 +0800
Processing by Admin::MentorshipTemplatesController#index as HTML
Completed 200 OK in 7759ms (Views: 732.5ms | ActiveRecord: 5539.6ms)   [mentorship_templates]

Started GET "/admin/substages/" for 162.213.96.35 at 2014-12-09 08:44:36 +0800  [blocked by the previous one]
Processing by Admin::SubstagesController#index as HTML
Completed 200 OK in 137ms (Views: 3.3ms | ActiveRecord: 126.2ms)
Started GET "/admin/statuses/?with_stages=true" for 162.213.96.35 at 2014-12-09 08:44:36 +0800
Processing by Admin::StatusesController#index as HTML
  Parameters: {"with_stages"=>"true"}
Connecting to database specified by database.yml      [didn't see this in the previous log]
Completed 200 OK in 6167ms (Views: 275.2ms | ActiveRecord: 5377.1ms)  [statuses]

Started GET "/admin/mentorships.json" for 162.213.96.35 at 2014-12-09 08:44:43 +0800 [blocked by the previous one]
Processing by Admin::MentorshipsController#index as JSON

Started GET "/major_aliases/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:44:48 +0800 [5 seconds delay. although the previous one didn't complete yet]
Started GET "/institutes_aliases/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:44:48 +0800
Started GET "/admin/applicants/list_for_typeahead" for 162.213.96.35 at 2014-12-09 08:44:48 +0800
Started GET "/admin/advisors/list_for_ajax" for 162.213.96.35 at 2014-12-09 08:44:49 +0800
Processing by MajorAliasesController#list_for_typeahead as HTML
Processing by InstitutesAliasesController#list_for_typeahead as HTML
Processing by Admin::ApplicantsController#list_for_typeahead as HTML
Processing by Admin::AdvisorsController#list_for_ajax as HTML

Read fragment views/palmdrive.cn/institutes_aliases/list_for_typeahead (0.6ms)
Completed 200 OK in 987ms (ActiveRecord: 183.7ms)
Completed 200 OK in 1716ms (Views: 15.2ms | ActiveRecord: 282.6ms)
Completed 200 OK in 1351ms (Views: 200.4ms | ActiveRecord: 269.5ms)
Completed 200 OK in 1938ms (Views: 342.7ms | ActiveRecord: 388.6ms) 
Completed 200 OK in 15694ms (Views: 286.0ms | ActiveRecord: 12694.9ms) [mentorships]

看起来昂贵的数据库阻止服务器处理其他请求。这让我很困惑!

它是一个rails应用程序,使用Passenger Nginx作为生产服务器,数据库是mysql。我想知道对此有什么好的解释。 Passenger Nginx如何处理并发请求?

1 个答案:

答案 0 :(得分:0)

Passenger(在其开源版本中)没有使用多线程。通过启动许多进程来处理并发。 您的请求可以由任何正在运行的进程提供。如果您的请求全部由同一进程处理,则它们将相互阻止。假设您有两个乘客流程,那么将有50%的机会由同一流程处理。

提高绩效:

  • 运行更多进程以降低请求阻塞对方的可能性

  • 获取Passenger Enterprise并启用可提供更高性能的多线程

您可能还想检查: https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#PassengerConcurrencyModel