Ming
Ming

Reputation: 1693

Using Sinatra for Building Web Proxy, Handling I/O Blocking? (+ Strange Heroku request concurrency issue)

I want to write something similar to an HTTP web proxy. I am currently exploring using Ruby with Sinatra for this.

One of my main worries with this is that the HTTP request I make to another server might take a while to come back. How do ensure I can continue to serve other requests in the meantime?

I created an extremely contrived example that simulates this sort of blocking.

hello.rb:

require 'rubygems'
require 'sinatra'

set :server, 'thin'

get '/fast' do
    'Fast Hello World!'
end

get '/slow' do
    sleep 10 
    'Slow Hello World!'
end

config.ru:

require './hello'
run Sinatra::Application

What has me absolutely baffled is this. If I run this locally with "ruby hello.rb", accesing /slow will not interfere with accessing /fast. However, if I deploy to Heroku, then suddenly, requests to /fast will wait for requests to /slow to finish up. I had thought that both locally and on Heroku, this app was running on single-threaded Thin. I cannot understand why I get different behavior.

I am pretty new to all this. Why am I experiencing this inconsistent behavior? What would be a good way to accomplish what I am trying to do?

Thanks!

Upvotes: 1

Views: 499

Answers (1)

coolesting
coolesting

Reputation: 1451

Because the command "ruby hello.rb" will shutdown the web server of thin after you run a request, but the heroku will keep your ruby thread alive on web server, so you could try more time with the HTTP request.

You could do it with this thin start(make sure your current directory has the config.ru) if you have installed the server thin, you will see the resulte as the heroku.

Upvotes: 1

Related Questions