Question

I'm attempting to put a small payload generated in route A (Sinatra app) to Route B using Faraday. So the code basically looks like:

post "/routeA" do
  foo.save
  foo_id = foo.id
  conn = Faraday.new(:url => "http://localhost:3001/routeB" ) do |builder|
    builder.request :url_encoded
    builder.response :logger
    builder.adapter :net_http
  end

  resp = conn.put do |req|
    req.url '/routeB'
    req.headers['Content-Type'] = 'application/json'
    req.body = {:id => foo_id }.to_json
    req.options = {
      #:timeout => 5,   # see below, these aren't the problem
      #:open_timeout => 2
    }
  end

  # never gets here b/c Timeout error always thrown
  STDERR.puts resp.body
end

put "/routeB" do
  # for test purposes just log output
  STDERR.puts request.body.read.to_s.inspect
  status 202
  body '{"Ok"}'
end

Problem is that it always throws a timeout error (I've run without the timeout options, and with the ones shown above -> same results). However, the logs show the request is going through:

I, [2012-03-24T16:56:13.241329 #17673]  INFO -- : put http://localhost:3001/routeB
D, [2012-03-24T16:56:13.241427 #17673] DEBUG -- request: Content-Type: "application/json"
#<Faraday::Error::TimeoutError>
DEBUG -     POST (60.7987ms) /routeA - 500 Internal Server Error
"{\"id\":7}"
DEBUG -      PUT (0.0117ms) /routeB - 202 Accepted

Not sure how to get past the timeout error? Any insight would be appreciated. Thanks.

Était-ce utile?

La solution

The problem is that the application cannot respond to another request until it's finished with the current one. That is, when you make a PUT request on the /routeB, the application got that, and it's waiting for the current request (/routeA) to finish. But the request won't finish because it's waiting to get the response from the /routeB. I think this is what causes the timeout error.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top