2

I have problem creating http request inside my controller action. I used net/http and RestClient but I can't get it to work on my local server url i.e http://localhost:3000/engine/do_process, I always get requesttimeout however It works with other valid url.

Hope you can enlighten me on this one. I did some research but I can't find resources as to why I got this timeout problem.

Sample controller code:

require 'rest_client'

class LgController < ApplicationController
  def get_lgjson

     response =  RestClient.get("http://localhost:3000/engine/do_process_lg")
     @generated_json = response.to_str

  end
end
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
jemtech008
  • 41
  • 1
  • 4
  • 4
    Show us the code to create the request please. My money is on that you're making another HTTP request to another part of your application and because WEBrick being a single-threaded server it's not able to serve it at the same time as the initial request. – Ryan Bigg Oct 07 '11 at 03:11
  • require 'rest_client' class LgController < ApplicationController ef get_lgjson response = RestClient.get("http://localhost:3000/engine/do_process_lg") @generated_json = response.to_str end end – jemtech008 Oct 07 '11 at 03:14
  • I always get request timeout problem, not sure why. – jemtech008 Oct 07 '11 at 03:17
  • 1
    What Ryan Bigg said: "it's not able to serve it at the same time as the initial request". That means RestClient#get cannot be responded to until your get_lgjson controller action finishes (i.e. never).... You want a multi threaded server, or maybe run two instances on different ports. – adzdavies Oct 07 '11 at 03:50
  • @adzdavies thanks for the reply. I think i need a multi threaded server. Its my first time coding RoRs, i don't know which way to go. – jemtech008 Oct 07 '11 at 04:11

2 Answers2

1

I encountered this problem today, too, exactly in the same context: using the Ruby RestClient to make a HTTP request inside a controller. It worked earlier in a different project using OpenURI without problems. This was surprising because both http libraries, the RestClient and OpenURI for Ruby, use the same library Net::HTTP.

It is the URL that makes the difference. We can make a connection to an external URL in the controller, but not to localhost. The problem seems to be the duplicated connection to localhost. There is already a connection to localhost open, and we are trying to open a second one. This does not seem to work in a single-threaded web server like Thin for instance. A multi-threaded web server such as Puma could help.

0x4a6f4672
  • 27,297
  • 17
  • 103
  • 140
0

I think this is because you use single-threaded web server. You have two opportunities to fix.

  1. use passenger

  2. define if it makes sense to make net/http to localhost.

Mike
  • 1