2

I have problems understanding what is happening when calling external APIs using the fibers model with eventmachine. I have this code in Sinatra:

get '/' do
  conn = Faraday.new 'http://slow-api-call' do |con|
    con.adapter :em_http
  end
  resp = conn.get
  resp.on_complete {
    request.env['async.callback'].call(resp)
  }
  throw :async
end

Also, I am booting Rainbows server using the :EventMachine connector with 2 connections (that means 2 fibers handling 2 http requests at a time).

Now, If I made 4 concurrent requests, the app should manage 2 at first, and when the external API calls are being made, those fibers should be able to manage 2 new http requests while waiting for the external call to finish, right?

This is not happening. No new http requests are being accepted until the slowapi call returns and free up the Fiber.

Is this the correct behavior? Am I missing something? Thanks.

Ron
  • 2,215
  • 3
  • 22
  • 30

1 Answers1

1

Actually, this was the correct behaviour.

When configuring Rainbows to handle 2 http requests using 2 fibers, it actually means that the number of incoming http requests in being limited to 2.

So, the resources being used by the fibers while the slow API is called are free (memory, files, database connections, etc), but the server is not accepting more than 2 http connections and those free fibers cannot actually process anything.

Rainbows should point this out more clearly in the documentation. Will send them an email.

Hope this helps somebody.

Ron
  • 2,215
  • 3
  • 22
  • 30