No perfomance gains from using em-http-request

被刻印的时光 ゝ 提交于 2019-12-11 09:32:27

问题


I'm trying to understand how to use various non-blocking IO libraries in Ruby and made a simple app for testing using Sinatra,

# proxy.rb
require 'bundler/setup'
require 'sinatra/base'
require 'sinatra/synchrony'
require 'faraday'

class ProxyApp < Sinatra::Base
  register Sinatra::Synchrony

  get "/proxy" do
    conn = Faraday.new("http://mirror.yandex.ru") do |faraday|
      faraday.use Faraday::Adapter::EMSynchrony
    end
    conn.get "/ubuntu-releases/precise/ubuntu-12.04.1-alternate-i386.iso"
    "Hello, world"
  end

  get "/" do
    "Hello, world"
  end
end

As far as I understand, downloading a file using non-blocking IO should allow other requests to execute, but it doesn't - if I'm using ab to open /proxy path (I'm using Thin as an app server), request to / takes a very long time. Am I doing something wrong?


回答1:


Sinatra::Synchrony? Why?

config.ru:

require File.join Dir.pwd, 'proxy.rb'
run Proxy

Gemfile:

source 'https://rubygems.org'

gem 'sinatra'
gem 'thin'
gem 'faraday'
gem 'em-synchrony'
gem 'em-http-request'
gem 'rack-fiber_pool'

proxy.rb:

require 'bundler'
Bundler.require

class Proxy < Sinatra::Base
  use Rack::FiberPool

  get "/proxy" do
    conn = Faraday.new("http://mirror.yandex.ru") do |faraday|
      faraday.use Faraday::Adapter::EMSynchrony
    end
    conn.get "/ubuntu-releases/precise/ubuntu-12.04.1-alternate-i386.iso"
    "Hello, world"
  end

  get "/" do
    "Hello, world"
  end
end

Start:

thin start -d
wget localhost:3000/proxy

In another terminal:

wget localhost:3000/

The reply is immediate for /, no matter how many requests to /proxy you do in parrallel.



来源:https://stackoverflow.com/questions/12530853/no-perfomance-gains-from-using-em-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!