2012-03-24 57 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獲得響應。我認爲這是造成超時錯誤的原因。