为什么 puma 不使用它的所有线程?

DrF*_*son 6 nginx web-server ruby ruby-on-rails

我正在使用 puma 和 nxinx,据我所知,即使我以 16 个或更多线程的默认值启动它,它也只使用单个线程。我已经设置了一个新的 rails 应用程序,然后完成了这里描述的设置:

http://blog.wiemann.name/rails-server

这给出了这个示例 nginx 配置:

upstream benchmarkapp.com {server unix:/tmp/benchmark_app.sock fail_timeout=0;}
server {
 server_name benchmarkapp.com;
 root /home/tristan/benchmark_app/public;
 try_files $uri/index.html $uri @benchmarkapp.com;
 location @benchmarkapp.com {
 proxy_redirect off;
 proxy_pass http://benchmarkapp.com;
 }
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个简单的控制器动作,它只是休眠 3 秒,然后呈现“hello”:

class WelcomeController < ApplicationController
  def index
    sleep(2)
    render :text => "hello"   
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我开始使用 puma: puma -t 16 -b unix:///tmp/benchmark_app.sock -S /tmp/benchmark_app.state

一旦运行,我使用 siege 用 10 个并发用户命中它,这是结果

 % siege -c 10 -t 60s http://benchmarkapp.com
** SIEGE 2.70
** Preparing 10 concurrent users for battle.
The server is now under siege...
HTTP/1.1 200   2.04 secs:      25 bytes ==> /
HTTP/1.1 200   4.05 secs:      25 bytes ==> /
HTTP/1.1 200   6.06 secs:      25 bytes ==> /
HTTP/1.1 200   8.08 secs:      25 bytes ==> /
HTTP/1.1 200  10.09 secs:      25 bytes ==> /
Run Code Online (Sandbox Code Playgroud)

如果应用程序运行单线程,这正是我期望看到的。接受前两个请求。他们大约在同一时间到达。第一个需要两秒钟,到目前为止还不错。但是第二个、第三个、...到第 10 个都必须为每个请求额外等待 2 秒。事实上,如果我重新启动美洲狮只有1个线程我得到正是这样的结果。

我在这里做错了什么?如何让服务器实际使用 puma 产生的所有线程?如果一切正常,我希望看到:

 % siege -c 10 -t 60s http://benchmarkapp.com
** SIEGE 2.70
** Preparing 10 concurrent users for battle.
The server is now under siege...
HTTP/1.1 200   2.04 secs:      25 bytes ==> /
HTTP/1.1 200   2.05 secs:      25 bytes ==> /
HTTP/1.1 200   2.03 secs:      25 bytes ==> /
HTTP/1.1 200   2.01 secs:      25 bytes ==> /
HTTP/1.1 200   2.06 secs:      25 bytes ==> /
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

小智 0

在网上做了很多研究并试图弄清楚如何让 Puma 实际使用线程后,我终于弄清楚了。

config.allow_concurrency = true您可以在应用程序配置中使用。这将消除中间件的使用,Rack::Lock该中间件负责使用 GIL 来保证 ruby​​ 中的线程安全(它确保一次只有 1 个线程运行)。

要验证您的配置,请使用rake middleware并确保您没有use Rack::Lock在列表中看到。