2012-03-24 95 views
1

我正在尝试使用法拉第将路由A(Sinatra应用程序)中生成的小负载放到路由B.因此,代码基本上看起来像:法拉第(红宝石)超时错误

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 

问题是,它总是抛出一个超时错误(我没有超时的选项来运行,并与上面显示的那些 - >相同的结果)。但是,日志显示请求正在经历:

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 

不确定如何超过超时错误?任何洞察力将不胜感激。谢谢。

回答

3

问题是,应用程序无法响应另一个请求,直到它完成当前的一个。也就是说,当您在/routeB上发出PUT请求时,应用程序已获得该请求,并且正在等待当前请求(/routeA)完成。但是请求无法完成,因为它正在等待从/routeB获得响应。我认为这是造成超时错误的原因。